You are currently viewing Python String endswith

Python str.endswith() is a built-in method that is used to check whether a given string ends with a specified suffix. It returns True if the string ends with the provided suffix, otherwise returns False.

Advertisements

In this article, I will explain the Python string endswith() method and using its syntax, parameters, and usage how we can check whether a specified string ends with a given suffix or not with examples.

1. Quick Examples of String endswith

If you are in a hurry, below are some quick examples of string endswith() method.


# Quick examples of string endswith() function

# Initialization of the string
string = "Welcome To SparkByExamples"

# Example 1: Check if the string ends with "SparkByExamples"
result = string.endswith("SparkByExamples")

# Example 2: Check if the string ends with "Welcome"
result = string.endswith("Welcome")

# Example 3: Check if the substring endining 
# From index 9 starts with "sparkByExamples"
result = string.endswith("SparkByExamples", 9)

# Example 4: Use endswith() with start and end Parameters
result = string.endswith("SparkByExamples", 7, 16)

# Example 5: Check if the substring ending returns True
result = string.endswith("SparkByExamples", 9, 27)

# Example 6: Check if the string ends with any of the suffixes
suffixes = ("Examples", "Python", "SparkByExamples")
result = string.endswith(suffixes)

2. Python String endswith() Method

The str.endswith() method is used to determine whether a string ends with a specified suffix or not. If the string ends with the provided suffix, it returns True otherwise it returns False.

2.1 Syntax of String endswith() Method

Following is the syntax of the string endswith() method.


# Syntax of string.endswith() function
string.endswith(suffix, start, end)

2.2 Parameter of String ensswith()

  • suffix – This is the substring that you want to check if the string ends with.
  • start (optional) – The starting index within the string where the checking starts. By default, it starts from the beginning of the string (index 0).
  • end (optional) – The ending index within the string where the checking stops. By default, it checks up to the end of the string.

2.3 Return Value

It returns True if a string ends with the specified value (suffix), and it returns False if the string does not end with the specified value.

3. Use String endswith() Without Start and End Parameters

You can use the endswith() method without providing the optional start and end parameters. When you omit these parameters, the method will consider the entire string for the check.

In the below example, you’re initializing a string with the value “Welcome To SparkByExamples”. Then, you’re using the endswith() method to check whether the string ends with the specified suffix “SparkByExamples”. Since the string does indeed end with “SparkByExamples”, the result will be True.


# Initialization the string
string = "Welcome To SparkByExamples"
print("Original string:", string)

# Check if the string ends with "SparkByExamples"
result = string.endswith("SparkByExamples")
print("Check the string ends with specified suffix:", result)

Yields below output.

python string endswith

Another example is that you can check if a string ends with the specified suffix. For example, you’re initializing a string with the value "Welcome To SparkByExamples". Then, you’re using the endswith() method to check whether the string ends with the specified suffix "Welcome". Since the string does not end with "Welcome", the result will be False.


# Initialization the string
string = "Welcome To SparkByExamples"
print("Original string:", string)

# Check if the string ends with "Welcome"
result = string.endswith("Welcome")
print("Check the string ends with specified suffix:", result)

Yields below output.

python string endswith

4. Use endswith() With Start and End Parameters

Alternatively, you can use the str.endswith() method with the start parameter to check the string ends with a specified suffix. However, please note that the start parameter specifies the index from which to start checking the substring for the given suffix, not the index from which the substring itself starts.

In the below example, the start parameter value of 9 indicates that you want to check if the substring starting from index 9 onwards (i.e., “omTo SparkByExamples”) ends with the specified suffix “SparkByExamples”.


# Initialization string
string = "Welcome To SparkByExamples"
print("Original string:", string)

# Check if the substring endining 
# From index 9 starts with "sparkByExamples"
result = string.endswith("SparkByExamples", 9)
print("Check the string ends with specified suffix:", result)

# Output:
# Original string: Welcome To SparkByExamples
# Check the string ends with specified suffix: True

You can use the endswith() function with start and end parameters to check whether a given string ends with a specified substring. In your program, you are using the endswith() method with the start and end parameters to check a specific portion of the string.

You are checking whether the substring of the original string from index 7 to index 16 (end index is exclusive) ends with “SparkByExamples”. Here, the substring “To SparkBy” is being considered for the check. Since “To SparkBy” doesn’t end with “SparkByExamples”, the result will be False.


# Initialization of the string
string = "Welcome To SparkByExamples"
print("Original string:", string)

# Use endswith() with start and end Parameters
result = string.endswith("SparkByExamples", 7, 16)
print("Result with start and end parameters:", result)

# Output:
# Original string: Welcome To SparkByExamples
# Result with start and end parameters: False

You are checking whether the substring of the original string from the index 9 to index 27 (end index is exclusive) ends with “SparkByExamples”. Since the substring “kByExamples” indeed ends with “SparkByExamples”, the result will be True.


# Initialization of the string
string = "Welcome To SparkByExamples"
print("Original string:", string)

# Check if the substring ending returns True
result = string.endswith("SparkByExamples", 9, 27)
print("Result with start and end parameters:", result)

# Output:
# Original string: Welcome To SparkByExamples
# Result with start and end parameters: True

5. Using endswith() With Tuple Suffix

You can also use the endswith() method with a tuple of suffixes to check if a string ends with any of the specified suffixes. For example, the code checks whether the string ends with any of the specified suffixes. Since the string “Welcome To SparkByExamples” does end with “SparkByExamples”, the result will be True.

Even though “SparkByExamples” is the only suffix that matches in this case, the method will return True if any of the provided suffixes match the end of the string.


# Initialization of the string
string = "Welcome To SparkByExamples"
print("Original string:", string)

# Suffixes to check
suffixes = ("Examples", "Python", "SparkByExamples")

# Check if the string ends with any of the suffixes
result = string.endswith(suffixes)
print("Result with tuple of suffixes:", result)

# Output:
# Original string: Welcome To SparkByExamples
# Result with tuple of suffixes: True

Conclusion

In this article, I have explained the Python string endswith() method and using its syntax, parameters, and usage how we can check the string ends with specified suffix with examples. And also explained how it return a boolean value like True, or False when satisfied with a specified suffix.

Happy Learning !!