Python String endswith() method - Tutorial Kart
Python String endswith() method
Python String endswith()
Python String endswith() method returns True if the string ends with the specified value, or False if the string does not end with the specified value. In this tutorial, we will learn the syntax and examples for endswith() method of String class.
Syntax
The syntax of String endswith() method in Python is str.center(value, start, end)
where
Parameter Required/Optional Description
value s t a rt end
Required Optional Optional
A string. The value which is used to match the end of string. An integer. The index in string, from which search has to happen. An integer. The index in string, until which search has to happen.
Example
In this example, we will take a string 'abcdef' , and check if this string ends with the value 'ef' .
Python Program
x = 'abcdef' value = 'ef' result = x.endswith(value) print(result)
Output True
endswith() with Specific Start
In the following program, we will specify a starting position/index from which the search for the value value in the string has to happen.
Python Program
x = 'abcdef' value = 'ef' start = 5 result = x.endswith(value, start) print(result)
Output False
Explanation
When search starts from index 5, there is match with the specified value. Therefore, endsWith() returns False.
'a b c d e f' 0 1 2 3 4 5
| start=5
endswith() with Specific Start and End
In the following program, we will specify a starting position/index from which the search for the value value in the string has to happen and the end position up until which the search for value in the string has to happen.
Python Program
x = 'abcdef' value = 'ef' start = 1 end = 5 result = x.endswith(value, start, end) print(result)
Output False
Explanation
When search starts from index 1 and can be done only till index 5, there is no match with the specified value. Therefore, endsWith() returns False.
'a b c d e f'
0 1 2 3 4 5
|
|
start=1
end=5
b c d e is the searchable string
Now, for the same start and end value, let us check if the string ends with the value 'de' .
Python Program
x = 'abcdef' value = 'de' start = 1 end = 5 result = x.endswith(value, start, end) print(result)
Output True
Explanation
When search starts from index 1 and can be done only till index 5, there is match with the specified value. Therefore, endsWith() returns True.
'a b c d e f'
0 1 2 3 4 5
|
|
start=1
end=5
b c d e is the searchable string
d e ends with specified value
Conclusion
In this Python Tutorial, we learned how to check if given string ends with specified value using String method ? endswith().
Python Programming
Python Strings Python String capitalize()
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- python string char
- python string find
- python string to char
- python string find replace
- python string to char list
- python string array to string
- python string split each character
- python string encoding utf8
- python string methods
- python get current method name
- python call class method dynamically
- python class and method examples