Python String – endswith() method

This is a string method in python that returns True or False if the string ends with the specified value.

Syntax:

string.endswith(value, starting_position, ending_position)

Example 1:

s = “CodeWindow offers IT related news.”
x = s.endswith(“news.”,5,34)
print(x)

Output:

True

Explanation:

Here the string ends with “news.” starting from index 5 to index 34-1. Hence it returns True.

Example 2:

s = “CodeWindow offers IT related updates.”
x = s.endswith(“news.“)
print(x)

Output:

False

Explanation:

Here the string ends with “updates.” but the value provided here is “news.”. Hence it returns False.

Example 3:

s = “CodeWindow offers IT related news
x = s.endswith(“news.“)
print(x)

Output:

False

Explanation:

Here the string ends with only “news” but the value provided here is “news.” (An extras “.” In the end). Hence it returns False.

You Missed