Python String endswith() Method

By Lenin Mishra

If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.

The endswith() method in Python returns True if the string ends with the specified value, otherwise, returns False. It is not case sensitive.

Syntax of endswith() method

string.endswith(value, start, end)

value: Value to check if the string endswith(Required)
start: The integer position to start the search. Default is 0.(Optional)
end:   The integer position to end the search. Default is the end of the string.(Optional)
Example 1

Code

str1 = "Pylenin makes videos on Python"

print(str1.endswith('Python'))
print(str1.endswith('videos'))

Output

True
False
Example 2 - Check for case sensitivity

Code

str1 = "Pylenin makes videos on Python"

print(str1.endswith('python'))

Output

False
Example 3 - Check within specified positions of a string

Code

str1 = "Pylenin makes videos on Python"

# Check between position 5 and 10
print(str1.endswith('Python', 5, 10))

print(f"It occurs at position - {str1.find('Python')}")

Output

False
It occurs at position - 24

Check out other commonly used Python string methods.

  1. How to create a string in Python?
  2. How to access characters in a Python string?
  3. How to replace characters in a string in Python?
  4. How to concatenate strings in Python?
  5. How to iterate through a string in Python?
  6. Check if a Substring is Present in a Given String in Python
  7. Escape sequences in Python String
  8. Python String Formatting - The Definitive Guide