python
python - startswith, endswith
yscho03
2020. 12. 18. 13:07
728x90
반응형
startswith
접두어 부분 매칭 여부를 판단할때 매우 유용하다
str = 'Hello world!'
print( str.startswith( 'Hello' ) ) # True
print( str.startswith( 'He' ) ) # True
str = 'Hello world!'
print( str.startswith( 'll' ) ) # False
print( str.startswith( 'll', 2 ) ) # True
str = 'Hello world!'
print( str.startswith( 'Hell', 0, 3 ) ) # False
print( str.startswith( 'Hell', 0, 4 ) ) # True
endswith
접미어 부분 매칭 여부를 판단할때 매우 유용하다
print( 'world'.endswith('world') ); # True
print( 'hello world'.endswith('world') ); # True
print( 'hello worl'.endswith('hello') ); # False
import re
def my_endswith(needle, haystack):
return not not re.match(r'.*'+needle+'$', haystack)
print( my_endswith('world', 'world') ); # True
print( my_endswith('world', 'hello world') ); # True
print( my_endswith('world', 'hello worl') ); # False
출처
728x90
반응형