스트링에 특정 단어 위치 찾기
조회수 29241회
1 답변
-
string.find()가 해당 기능을 지원합니다. string.find(substring)는 substring이 존재하면 시작 인덱스를, 존재하지 않으면 -1을 리턴 해줍니다.
st1 = "Hello World!" st2 = "World" idx = st1.find(st2) if idx==-1: print('not exist') else: print('exist at idx', idx)
과 같이 쓰시면 됩니다.
존재 유무만 판단하고 싶을 때는 "in" 오퍼레이터를 쓰셔도 됩니다.
st1 = "Hello World!" st2 = "World" if st2 in st1: print('exist') else: print('not exist')
댓글 입력