The endswith()
method in Python is a string method that returns True
if a string ends with a specified suffix, otherwise it returns False
. It is used to check if a string ends with a particular substring.
Parameter Values
Parameter | Description |
---|---|
suffix | The |
start | The |
end | The |
Return Values
The endswith()
method returns a bool
value: True
or False
.
How to Use endswith()
in Python
The endswith()
method returns True if a string ends with the specified suffix, otherwise it returns False.
word = 'Hello World'
print(word.endswith('World'))
The endswith()
method can also take a tuple of suffixes as argument to check if the string ends with any of them.
file_name = 'document.txt'
print(file_name.endswith(('.txt', '.pdf')))
The endswith()
method is case sensitive by default, but you can use case-insensitive comparison by converting the string to lowercase.
text = 'Python Programming'
print(text.lower().endswith('programming'))