Python String endswith()
Python string endswith()
is an inbuilt function which returns true
in the case, if the string ends with a particular specified suffix; else it will returns false
.
-
In this case, if the start and end index is not specified then by default start is taken as 0
and length is taken as -1
and ending index is not included in our search.
-
If the string is ending with a suffix that is used in endswith()
function then the function will returns true
otherwise, it will return false
.
-
If you want to check that string ends with one of the strings from the list then in that case string endswith()
function is used.
-
This method returns the boolean value.
-
This function also accepts tuple; we will show you with the help of examples
Python String endswith()
: Syntax
Below we have a basic syntax of String endswith()
in Python:
string.endswith(suffix, start, end)
Python String endswith()
: Parameters
The description of the Parameters of endswith()
is given below:
This parameter is used to specify the part of the string for which we will check if the string is ending with that part or not.
It is an optional parameter and from here suffix will start.
It is an optional parameter and the suffix will end here.
Python String endswith()
: Returned Values
For the returned value there are two cases:
Python String endswith()
: Basic Example
Below we have an example to show the working of String endswith()
function:
string = "Hello I am Ram and I am a developer"
result = string.endswith("per")
result1 = string.endswith("PER")
print("Comparing it with right suffix so output: ",result)
print("Comparing it with wrong suffix so output: ",result1)
The Output for the same is given below:
Comparing it with right suffix so output: True
Comparing it with wrong suffix so output: False
Python String endswith()
: Using start and end Parameters
In the example given below, we will use both start and end parameter of endswith()
. Let us see the code snippet:
abc = "This is a string and is awesome"
right_suffix = "some"
wrong_suffix = "some!"
print(abc.endswith(right_suffix, 28, 31))
print(abc.endswith(wrong_suffix, 28, 31))
The Output for the same is given below:
false
false
Python String endswith()
: Tuple Sufix
In this example, we are checking if the string ends with one of the strings in the Tuple. Let us see the code snippet:
text = "StudyTonight"
result = text.endswith(('Study', 'Otis'))
print(result)
result = text.endswith(('Study', 'night'))
print(result)
The Output for the same is given below:
false
true
Python String endswith()
: Using List
In this example, we are checking if the string ends with one of the strings in the List. Let us see the code snippet:
ext = ['.jpg','.png']
file = 'test.png'
print(file.endswith(('.jpg', '.png')))
The Output for the same is given below:
true
Time For Live Example!
Now let us take a look at a live example so as to understand the endswith()
method completely:
Summary
In this tutorial, we learned endswith()
method with a description of its parameters and returned values. We have also used tuple and list as a suffix. Also, there is a live example of endswith()
method.