i2tutorials

Endswith() method in Python String Module

Endswith() method in Python String Module


Endswith method gives the output as TRUE if the string ends with the designated or specified suffix else it gives FALSE.


Syntax:

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


Parameters:

Endswith has 3 arguments, suffix, start and end. Here start and end are optional arguments.

  1. Suffix: Here the suffix of string or tuple is checked.
  2. Start: Here suffix is checked at the beginning position in the string.
  3. End: Here suffix is checked at the end position within the string.


Example-1:

text = "The Rigi and Pilatus mountains nearby in Switzerland offer skiing and hiking.”
x = text.endswith("hiking.")
print(x)


Output:


Example-2:

text = "The mountains nearby in Switzerland offer peace.”
x = text.endswith("offer")
print(x)


Output:


Example 1: Endswith()  with suffix, start and end

Here start and end arguments are given correct indexing values

text = "The Rigi and Pilatus mountains nearby in Switzerland offer skiing and hiking."
x = text.endswith("hiking.", 17, 77)
print(x)


Output:


Example-2:

Here only start argument is given the correct indexing value, end argument is not given the correct indexing value.

text = "The Rigi and Pilatus mountains nearby in Switzerland offer skiing and hiking."
x = text.endswith("hiking.", 17, 75)
print(x)


Output:


Example-3:

Here start argument is not given. We provided only end argument with correct indexing value.

text = "The Rigi and Pilatus mountains nearby in Switzerland offer skiing and hiking."
x = text.endswith("hiking.", 75)
print(x)


Output:

Exit mobile version