Python에는 문자열에 하위 문자열이 포함되어 있는지 확인하기 위한 전용 string.contains() 또는 string.indexof() 메서드가 없습니다. . 그러나 이 기능을 구현하기 위한 다른 접근 방식이 있습니다.
'in' 연산자를 사용하면 문자열 내에 하위 문자열이 있는지 테스트할 수 있습니다. 하위 문자열을 전체 문자열과 비교하여 해당 문자열이 있으면 True를 반환합니다.
if "blah" in some_string: # Code to be executed if "blah" is in some_string else: # Code to be executed if "blah" is not in some_string
참고: 'in' 연산자는 대소문자를 구분합니다.
string.find() 메서드: 하위 문자열이 처음 나타나는 인덱스를 반환하거나, 찾을 수 없으면 -1을 반환합니다.
index = some_string.find("blah")
string.startswith() 메서드: 문자열이 지정된 문자열로 시작하는지 확인합니다. substring.
if some_string.startswith("blah"): # Code to be executed if some_string starts with "blah"
string.endswith() 메서드: 문자열이 지정된 하위 문자열로 끝나는지 확인합니다.
if some_string.endswith("blah"): # Code to be executed if some_string ends with "blah"
위 내용은 Python 문자열에 하위 문자열이 포함되어 있는지 어떻게 확인할 수 있나요?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!