Check string end with python

In this tutorial, we will learn about the Python String endswith() method with the help of examples.

The endswith() method returns True if a string ends with the specified suffix. If not, it returns False.

Example

message = 'Python is fun'

# check if the message ends with fun print(message.endswith('fun'))

# Output: True


Syntax of String endswith()

The syntax of endswith() is:

str.endswith(suffix[, start[, end]])

endswith() Parameters

The endswith() takes three parameters:

  • suffix - String or tuple of suffixes to be checked
  • start (optional) - Beginning position where suffix is to be checked within the string.
  • end (optional) - Ending position where suffix is to be checked within the string.

Return Value from endswith()

The endswith() method returns a boolean.

  • It returns True if a string ends with the specified suffix.
  • It returns False if a string doesn't end with the specified suffix.

Example 1: endswith() Without start and end Parameters

text = "Python is easy to learn."

result = text.endswith('to learn')

# returns False print(result)

result = text.endswith('to learn.')

# returns True print(result)

result = text.endswith('Python is easy to learn.')

# returns True print(result)

Output

False
True
True

Example 2: endswith() With start and end Parameters

text = "Python programming is easy to learn."

# start parameter: 7
# "programming is easy to learn." string is searched

result = text.endswith('learn.', 7)

print(result) # Both start and end is provided # start: 7, end: 26 # "programming is easy" string is searched

result = text.endswith('is', 7, 26)

# Returns False print(result)

result = text.endswith('easy', 7, 26)

# returns True print(result)

Output

True
False
True

Passing Tuple to endswith()

It's possible to pass a tuple suffix to the endswith() method in Python.

If the string ends with any item of the tuple, endswith() returns True. If not, it returns False


Example 3: endswith() With Tuple Suffix

text = "programming is easy"

result = text.endswith(('programming', 'python'))

# prints False print(result)

result = text.endswith(('python', 'easy', 'java'))

#prints True print(result) # With start and end parameter # 'programming is' string is checked

result = text.endswith(('is', 'an'), 0, 14)

# prints True print(result)

Output

False
True
True

If you need to check if a string starts with the specified prefix, you can use startswith() method in Python.



Description

Python string method endswith() returns True if the string ends with the specified suffix, otherwise return False optionally restricting the matching with the given indices start and end.

Syntax

str.endswith(suffix[, start[, end]])

Parameters

  • suffix − This could be a string or could also be a tuple of suffixes to look for.

  • start − The slice begins from here.

  • end − The slice ends here.

Return Value

TRUE if the string ends with the specified suffix, otherwise FALSE.

Example

#!/usr/bin/python

str = "this is string example....wow!!!";

suffix = "wow!!!";
print str.endswith(suffix)
print str.endswith(suffix,20)

suffix = "is";
print str.endswith(suffix, 2, 4)
print str.endswith(suffix, 2, 6)

Result

True
True
True
False

python_strings.htm

❮ String Methods


Example

Check if the string ends with a punctuation sign (.):

txt = "Hello, welcome to my world."

x = txt.endswith(".")

print(x)

Try it Yourself »


Definition and Usage

The endswith() method returns True if the string ends with the specified value, otherwise False.


Syntax

string.endswith(value, start, end)

Parameter Values

ParameterDescription
value Required. The value to check if the string ends with
start Optional. An Integer specifying at which position to start the search
end Optional. An Integer specifying at which position to end the search

More Examples

Example

Check if the string ends with the phrase "my world.":

txt = "Hello, welcome to my world."

x = txt.endswith("my world.")

print(x)

Try it Yourself »

Example

Check if position 5 to 11 ends with the phrase "my world.":

txt = "Hello, welcome to my world."

x = txt.endswith("my world.", 5, 11)

print(x)

Try it Yourself »


❮ String Methods


How do you check if a string ends in Python?

The endswith() function returns True if a string ends with the specified suffix (case-sensitive), otherwise returns False. A tuple of string elements can also be passed to check for multiple options. If a string ends with any element of the tuple then the endswith() function returns True.

How do you check the end of a string?

The endsWith() method returns true if a string ends with a specified string. Otherwise it returns false . The endsWith() method is case sensitive.

What is Endswith () in Python?

The endswith() method returns True if the string ends with the specified value, otherwise False.

How do you check if string ends with one of the strings from a list?

Use string..
a_string = "abc".
a_list = ["c", "d"].
string_tuple = tuple(a_list) Convert a_list to a tuple..
ends_with_string = a_string. endswith(string_tuple) Check if a_string ends with string_tuple..
print(ends_with_string).