Python String endswith() Method
The endswith() function checks if a string ends with a suffix. If so, endswith() will return the boolean True, otherwise it will return the boolean false.
How to formulate it like this:
string. endswith(suffix, start, end)
suffix: A word, string, or tuple that must be checked
start: [Optional] Start the index where the search starts.
end: [optional] The end of the index where the search stops.
Return value: Returns a boolean value that is either true or false.
Note: If the start and end index is not provided, it will default to 0 and the -1 character as the start and end index.
In the following example we will check if the string ends in 'obaid'. If it is, endswith() will return the boolean True, otherwise it will return the boolean false.
Example:
# Define a variable
string1 = "learn python with ahmad obaid"
# endswith a function call
print(string1.endswith("obaid")) # returns True
# Define a variable
string2 = "Hello Python"
# endswith a function call
print(string2.endswith("obaid")) # returns False
Will beundefined The resulting:
True
False
In the following example, we will check if the given word is between the start and end indexes. If so, the endswith() function will return the boolean True, and if not, it will return the boolean false.
Note: The reason for adding start and end values is that sometimes you need to supply a suffix/capital to be checked. Start and end parameters help you with this.
Example:
# Define a variable
string = "learn python with ahmad obaid"
# endswith a function call
print(string.endswith('obaid' , 22)) # returns True
# endswith a function call
print(string.endswith('obaid' , 22,30)) # returns True
# endswith a function call
print(string.endswith('obaid' , 22, 28)) # returns False
The output will be:
True
True
False
In the following example we will check if the string contains a tuple. That is, if the string ends with any element of the tuple, the endswith() function will return the boolean value True, and if it is otherwise, it will return the boolean value false
Example:
# Define a variable
string = "learn python with ahmad obaid"
# endswith a function call
result = string.endswith(( 'Hello', 'python', 'obaid', 'mango')) # returns True
# Print the result
print(result)
# Define a variable
string = "obaid"
# endswith a function call
result = string.endswith(('e', 'd', 'i', 'o', 'u')) # returns True
# Print the result
print(result)
# Define a variable
string = "python"
# endswith a function call
result = string.endswith(('e', 'a', 'i', 'o', 'u')) # returns False
# Print the result
print(result)
The output will be:
True
True
False
External sources:
Built-in functions - official Python documentation
If you have any questions or concerns, leave them in the comments
Tags
Python python data types Python String String comparison functions in Python Python String endswith() Method
Share page
About author
Ahmed Obaid
Hello, I am Ahmed Obaid, an Egyptian Arabic programmer. I would like to put my experiences learning Python on this site So that it will be a reference for you and me as well.
Sign up to leave a comment