파이썬 문자열 rindex() 메서드
예시
텍스트 에서 문자열 "casa" 가 마지막으로 나타나는 위치는 어디입니까?:
txt = "Mi
casa, su casa."
x = txt.rindex("casa")
print(x)
정의 및 사용
rindex()
메서드는 지정된 값의 마지막 항목을 찾습니다 .
rindex()
값을 찾을 수 없으면 메서드에서 예외가 발생합니다 .
방법 은 rindex()
방법과 거의 같습니다
rfind()
. 아래 예를 참조하십시오.
통사론
string.rindex(value, start, end)
매개변수 값
Parameter | Description |
---|---|
value | Required. The value to search for |
start | Optional. Where to start the search. Default is 0 |
end | Optional. Where to end the search. Default is to the end of the string |
더 많은 예
예시
텍스트에서 문자 "e"가 마지막으로 나타나는 위치는 어디입니까?:
txt = "Hello, welcome to my world."
x = txt.rindex("e")
print(x)
예시
위치 5와 10 사이에서만 검색할 때 텍스트에서 문자 "e"가 마지막으로 나타나는 위치는 무엇입니까?:
txt = "Hello, welcome to my world."
x = txt.rindex("e",
5, 10)
print(x)
예시
값을 찾을 수 없으면 rfind() 메서드는 -1을 반환하지만 rindex() 메서드는 예외를 발생시킵니다.
txt = "Hello, welcome to my world."
print(txt.rfind("q"))
print(txt.rindex("q"))