English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python 기본 강의

Python 흐름 제어

Python 함수

Python 데이터 타입

Python 파일 작업

Python 객체와 클래스

Python 날짜와 시간

Python 고급 지식

Python 참조 가이드

Python 문자열 endswith() 사용 방법 및 예제

파이썬 문자열 메서드

문자열이 지정된 값으로 끝나면 endswith() 메서드는 True를 반환합니다. 그렇지 않으면 False를 반환합니다.

endswith()의 문법은 다음과 같습니다:

str.endswith(suffix[, start[, end]])

endswith() 파라미터

endswith()는 세 가지 파라미터를 가집니다:

  • suffix -확인할 후缀 문자열 또는 튜플

  • start( 선택 )- 문자열에서 확인suffix시작 위치

  • end( 선택 )- 문자열에서 확인suffix끝 위치

endswith() 반환 값

endswith() 메서드는 부울 값을 반환합니다.

  • 지정된 값으로 끝나는 문자열이면 True를 반환합니다.

  • 지정된 값으로 끝나지 않는 문자열이면 False를 반환합니다.

예제1:끝과 시작 파라미터를 가지지 않는 endswith()

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

예제2:끝과 시작 파라미터를 가진 endswith()

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

튜플을 endswith()에 전달

Python에서 endswith() 메서드에 튜플을 지정된 값으로 전달할 수 있습니다

문자열이 튜플의 어떤 항목으로 끝나면 endswith()는 True를 반환합니다. 그렇지 않으면 False를 반환합니다

예제3:튜플의 endswith()를 포함

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() 메서드

파이썬 문자열 메서드