Language/Python
[Python/파이썬] 문자열에 기본으로 제공되는 내장함수
아이스베어 :)
2021. 6. 1. 15:11
728x90
반응형
* 본 포스팅은 주피터 노트북을 이용하였다. 파이참으로 실행해 보려면 이렇게
a란 변수를 추가하고 print(a)를 해줘야 볼 수 있을 것이다!
[ 대문자 소문자 변경 함수 ]
'hello'.upper() #'HELLO'
'HELLOW'.lower() #'hellow'
'hello'.capitalize() #'Hello'
[ 문자열 나누기(split), 결합하기(join), 세기(count), 교체하기(replace) ]
red = '안녕하세요,홍길동,바보'
result=red.split(',')
result # ['안녕하세요', '홍길동', '바보']
#배열에 저장된 문자열을 하나의 문자열로 결합
','.join(result) # '안녕하세요,홍길동,바보'
s1="aaaa"
s1.count('a') # 4
s1="aaaabbb"
s1.count('b') # 3
s1='Long Live the King'
s1.replace('King','홍길동') # 'Long Live the 홍길동'
s2="X:Y:Z"
s2.split(":") # ['X', 'Y', 'Z']
[ 문자열 format하기 ]
'I like {1} and {0}'.format('Python','java')
# 'I like java and Python'
'I like {} and {}'.format('Python','java')
# 'I like Python and java'
for i in range(2,15,2):
print('{0:3d} {1:4d} {2:5d}'.format(i,i*i,i*i*i))
# 2 4 8
# 4 16 64
# 6 36 216
# 8 64 512
# 10 100 1000
# 12 144 1728
# 14 196 2744
print('소수점 두자리 {0:3.2f}'.format(3.141592)) #소수점 두자리 3.14
[ 문자열 find(찾기), strip(공백제거)하기 ]
'hobby'.find('o') # 1
' hello '.rstrip() #오른쪽 공백제거
# ' hello'
' hello '.lstrip() #왼쪽 공백제거
# 'hello '
' hello '.strip() #공백제거
# 'hello'
728x90
반응형