59doit

[Python] 인덱싱 index(),find() 본문

Programming/Python(파이썬)

[Python] 인덱싱 index(),find()

yul_S2 2022. 10. 24. 09:45
반응형

문자열 위치 index

str= "hi. nice to meet you mina and yuna."
index = str.index("n")
print(index)   # <출력>  4

"n" 이라는 글자의 위치를 띄어쓰기 포함 하여 알려준다.

파이썬은 인덱싱이 0부터 시작이므로 n이 다섯번째에 있어서 결과값은 4가 나온다

 

 

str= "hi. nice to meet you mina and yuna."
index2 =str.index("n",index+1)  
print(index2)   # <출력> 23

str의 "n"을 찾는데 찾는값인 n의 index+1 은 n이 두번째로 있는 위치값을 찾아준다.

 

 

find

str= "hi. nice to meet you mina and yuna."
print(str.find("hara"))   # <출력> -1

값이 없으면 -1 을 출력한다.

 

 

 

 

 

반응형

'Programming > Python(파이썬)' 카테고리의 다른 글

[Python] 리스트  (0) 2022.10.27
[Python] 문자열 관련함수  (0) 2022.10.26
[Python] 포맷팅 format  (0) 2022.10.25
[Python] 인덱싱, 연산, 슬라이싱  (0) 2022.10.23
[Python] 줄 바꿈 , \b (삭제)  (0) 2022.10.22
Comments