본문 바로가기
Python

[Python] 문자열처리함수 : index와 find의 차이

by JJH0100 2022. 12. 17.
728x90
반응형

lower(), upper()

소문자, 대문자 출력

python = "Python IS Amazing"

#소문자 출력
print(python.lower())

#대문자 출력
print(python.upper())

#0번째 글자가 대문자인지 확인
print(python[0].isupper())
#0번째 글자가 소문자인지 확인
print(python[0].islower())

 

len()

글자 길이 확인(공백포함)

python = "Python IS Amazing"

print(len(python))

 

 

replace()

글자 대체

python = "Python IS Amazing"

#글자의 일부분만 다른 글자로 대체
print(python.replace("Python", "JAVA"))

 

index()

글자의 위치값 확인(0부터 시작됨)

python = "Python IS Amazing"

index = python.index("A")
print(index)
index = python.index("a")
print(index)
index = python.index("t")
print(index)

 

 

만약 글자의 다음번쨰 글자의 위치값을 알고 싶을 떄

python = "Python IS Amazing"

index = python.index("n")
print(index)
index = python.index("n", index + 1)
print(index)
index = python.index("i", index + 1)
print(index)

글자값을 찾을 수 없는 경우에는 에러가 난다.

 

 

 

find

index와 같은 역할을 한다.

다른 점은 index의 경우 지정한 문자를 찾을 수 없는 경우 에러가 나지만 

find는 지정한 문자를 찾지 못한 경우 -1 값을 반환한다는 것.

python = "Python IS Amazing"

#index와 비슷한 함수 find
print(python.find("n"))
#false일떄 -1 값 출력
print(python.find("Java"))
#에러남
print(python.index("Java"))

 

count()

해당 글자가 몇번 반복되었는지 세어준다.

python = "Python IS Amazing"

print(python.count("n"))

728x90
반응형

'Python' 카테고리의 다른 글

[Python] 리스트  (0) 2022.12.18
[Python] 문자열 포맷  (0) 2022.12.17
[Python] 슬라이싱  (0) 2022.12.17
[Python] random 함수  (0) 2022.12.17
[Python] 숫자처리 함수  (0) 2022.12.17

댓글