Python String endswith() Method
The str.endswith()
method returns True
if the string ends with the specified suffix; otherwise, it returns False
. The suffix can also be a tuple where multiple suffixes can be looked at.
Example:
text = 'Hello, World!'
print(text.endswith('World!'))
Output
True
Syntax
The Syntax of the str.endswith()
function is:
str.endswith(suffix, start=0, end=len(str))
Here, str
is a string.
Parameters
The str.endswith()
function can take three parameters.
- suffix - string or tuple of suffixes that need to be checked in a string
- start (optional) - The starting index of the string where the search should begin. The default is 0, which means the search starts at the beginning of the string if the argument is not passed.
- end (optional) - The ending index of the string where the search should end. The default is the end of the string, i.e.,
len(str)
, which means the search ends at the end of the string if the argument is not passed.
Return Value
The str.endswith()
function returns True
if the string ends with the provided suffix; otherwise, it returns False
.
Example 1: Check if a string ends with a specific string
The endswith()
method takes in a suffix string as its required argument, and optional start and end arguments return True
if the string ends with the provided suffix; otherwise, it returns False
.
Here are some examples to illustrate this:
text = 'Hello, World!'
# Check if the string ends with 'World!'
print(text.endswith('World!'))
# Check if the string ends with 'world!' (case-sensitive)
print(text.endswith('world!'))
# Check if the string ends with 'orld!' (starts the search from index 2)
print(text.endswith('orld!', 2))
# Check if the string ends with 'orld!' (starts the search from index 3)
print(text.endswith('orld!', 3))
# Check if the string ends with 'orld!' (starts the search from index 3 and ends at index 7)
print(text.endswith('orld!', 3, 7))
# Check if the string ends with 'orld!' (ends the search at index 6, which is before the suffix)
print(text.endswith('orld!', 0, 6))
# Check if the string ends with 'orld!' (starts the search from index 7, which is after the suffix)
print(text.endswith('orld!', 7))
Output
True
False
True
True
False
False
True
Example 2: Check if the file ends with a specific extension
In this example, the get_file_extension()
function checks if the file_name
argument ends with the ".txt" string. If it does, the function returns True
; otherwise it returns False
.
The code then calls the get_file_extension()
function and passes in the file_name
as an argument. If the function returns True
, the file extension is considered valid (ends with .txt); otherwise, it is considered invalid.
def get_file_extension(file_name):
if file_name.endswith(".txt"):
print(f"The file {file_name} is valid")
else:
raise ValueError(f"The file {file_name} is Invalid")
get_file_extension("myfile.txt") # This is fine
get_file_extension("myfile.doc") # This will raise a ValueError
Output
The file myfile.txt is valid
ValueError: The file myfile.doc is Invalid
Example 3:Validating an email address using endswith()
Method
# Check if the email is valid and ends with the domain specified
def is_valid_email(email):
return email.endswith('@example.com')
print(is_valid_email('user@example.com')) # Returns True
print(is_valid_email('user@gmail.com')) # Returns False
Output
True
False
Example 4:Validating a postal code using endswith()
Method
# Check if the email is valid and ends with the domain specified
def is_valid_postal_code(postal_code):
return postal_code.endswith(' USA')
print(is_valid_postal_code('90210 USA')) # Returns True
print(is_valid_postal_code('M4B 1B3 Canada')) # Returns False
Output
True
False
Reference: Python Official Docs