
str_ends_with 함수는 특정 문자열로 끝나는지 확인하는 함수입니다. 이 함수는 여러 개의 문자열을 확인할 수 있습니다.
str_ends_with 함수를 사용하여 두 개의 문자열이 특정 문자열로 끝나는지 확인하려면, 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
python
def ends_with(s, *suffixes):
for suffix in suffixes:
if s.endswith(suffix):
return True
return False
print(ends_with('hello', 'abc')) # True
print(ends_with('world', 'abc')) # False
위 코드에서, `ends_with` 함수는 여러 개의 문자열을 확인할 수 있습니다. `*suffixes`는 가변인자로, 함수를 호출할 때 여러 개의 문자열을 전달할 수 있습니다.
또한, `str.endswith` 메서드를 사용하여 더 간단하게 코드를 작성할 수 있습니다.
#hostingforum.kr
python
def ends_with(s, *suffixes):
return any(s.endswith(suffix) for suffix in suffixes)
print(ends_with('hello', 'abc')) # True
print(ends_with('world', 'abc')) # False
위 코드에서, `any` 함수를 사용하여 문자열이 특정 문자열로 끝나는지 확인합니다.
2025-08-15 10:11