English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
문자열이 지정된 값으로 끝나면 endswith() 메서드는 True를 반환합니다. 그렇지 않으면 False를 반환합니다.
endswith()의 문법은 다음과 같습니다:
str.endswith(suffix[, start[, end]])
endswith()는 세 가지 파라미터를 가집니다:
suffix -확인할 후缀 문자열 또는 튜플
start( 선택 )- 문자열에서 확인suffix시작 위치
end( 선택 )- 문자열에서 확인suffix끝 위치
endswith() 메서드는 부울 값을 반환합니다.
지정된 값으로 끝나는 문자열이면 True를 반환합니다.
지정된 값으로 끝나지 않는 문자열이면 False를 반환합니다.
text = "Python is easy to learn." result = text.endswith('to learn') # 반환 False print(result) result = text.endswith('to learn.') # 반환 True print(result) result = text.endswith('Python is easy to learn.') # 반환 True print(result)
프로그램을 실행할 때, 출력은 다음과 같습니다:
False True True
text = "Python programming is easy to learn." # start 파라미터: 7 # "programming is easy to learn."이 검색된 문자열입니다 result = text.endswith('learn.', 7) print(result) # start와 end 파라미터를 동시에 전달 # start: 7, end: 26 # "programming is easy"이 검색된 문자열입니다 result = text.endswith('is', 7, 26) # 반환 False print(result) result = text.endswith('easy', 7, 26) # 반환 True print(result)
프로그램을 실행할 때, 출력은 다음과 같습니다:
True False True
Python에서 endswith() 메서드에 튜플을 지정된 값으로 전달할 수 있습니다
문자열이 튜플의 어떤 항목으로 끝나면 endswith()는 True를 반환합니다. 그렇지 않으면 False를 반환합니다
text = "programming is easy" result = text.endswith(('programming', 'python')) #출력 False print(result) result = text.endswith(('python', 'easy', 'java')) #출력 True print(result) # start와 end 파라미터를 포함 # 'programming is' 문자열이 확인됩니다 result = text.endswith(('is', 'an'), 0,) 14) # 출력 True print(result)
프로그램을 실행할 때, 출력은 다음과 같습니다:
False True True
지정된 프리픽스로 시작하는 문자열을 확인하려면 파이썬에서 사용할 수 있습니다startswith() 메서드。