1. 타겟 문자열의 개수 세기, ( ).count('찾는문자열)
print("hobby".count('b)) => 2
혹은
a = "hobby"
print(a.count(''b)) = > 2
어디에 활용할지는 고민
2. 타겟 문자열의 위치 알려주기 (find)
sentence = "python is best choice"
print (sentence.find('best')) => 10 (0부터 시작)
3. 타겟 문자열의 위치 알려주기 (index)
sentence = "python is best choice"
print (sentence.index('best')) => 10 (0부터 시작)
find와 차이점은 거의 없지만,
find는 없는 문자를 찾으면 -1로 정상반환,
index는 에러를 발생시킨다. 뭐가 더 스탠다드일까?
4. 공백 지우기 (strip, lstrip. rstrip)
sentence = ' daniel '
print (sentence.strip()) => 'daniel' #양쪽 여백 지우기
print (sentence.lstrip()) ='daniel ' #왼쪽 여백 지우기
print (sentence.rstrip()) = ' daniel' #오른쪽 여백 지우기
양쪽 여백 지우기는 매우 자주할것 같다. 데이터 정규화에 필수
5. 대문자 소문자 바꾸기 (upper, lower)
sentence1 = 'you can do it'
sentence2 = 'You Can Do It'
print(sentence1.upper()) => "YOU CAN DO IT"
print(sentence2.lower()) => "you can do it"
6. 문자열 삽입 (join)
a.join('bbb') # 형태
a = '_x_' #삽입할 문자열
'bbb' = #삽입될 문자열
print (a.join('bbb')) = b_x_b_x_b
그런데 join은 어디다 쓰는 경우가 있을까?
스펠 단위로 문자열을 끼워넣는 경우가...
그리고 굉장이 자주 사용할수 밖에 없는 중요한 함수
7. 문자열 바꾸기 (replace)
sentence = 'life is good'
print ( sentence.replace('life', 'money') ) = > money is good
8. 문자열 나누기 (split)
sentence = 'life is good'
print (sentence.split(' ')) => ['life', 'is', 'good'] list로 분리된다.
print (sentence.split(' ')[1]) => 'is' (list중 두번째[1] 선택하기)
sentence = 'life is good'
splited = sentence.split(' ')
for word in splited:
print (word)
=> life
is
good
for문으로 단어별 분리됨
'취미로 하는 프로그래밍 > 짭짤한 파이썬' 카테고리의 다른 글
[requests] API를 호출해 보자, 너도 할 수 있어출처 - 2 (0) | 2020.10.09 |
---|---|
[requests] API를 호출해 보자, 너도 할 수 있어 (0) | 2020.10.08 |
파이썬 포매팅 (0) | 2020.08.11 |
까먹기 쉬운 필수 자료형 (2) - 슬라이싱 (0) | 2020.08.11 |
까먹기 쉬운 필수 자료형 (1) (0) | 2020.08.11 |