코테 준비 중, '문자열 때문에 파이썬으로 왔다'라는 말이 있을 만큼 파이썬에서는 문자열에 대해 유용한 기능이 많다.
그렇지만 자주 써먹지 않으면 잊어버려 이참에 정리를 했다.
a = "hello"
# 1
a.count("h")
# 2
a.count("l")
a = "hello"
# 2 -> 문자가 여러개인 경우 find,index는 모두 맨 앞의 위치를 가져온다.
a.find('l')
# 0
a.find('h')
find와 index의 차이는 찾는 문자가 없을 때 결과이다.
find는 -1 index는 오류가 발생한다.
하지만 index는 리스트, 튜플에서도 사용 가능
a = ['h','e','l','l','o']
# 'hello'
''.join(a)
# 'h,e,l,l,o'
','.join(a)
a = "helLo"
# HELLO
a.upper()
# helLo
print(a)
a = "helLo"
# hello
a.upper()
# helLo
print(a)
a = " hello"
# "hello"
a.lstrip()
# " hello"
print(a)
a = "hello "
# "hello"
a.rstrip()
# "hello "
print(a)
a = " hello "
# "hello"
a.strip()
# " hello "
print(a)
a = "hello"
# ['he', '', 'o']
a.split('l')