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 을 출력한다.

 

 

 

 

 

반응형