Python - Check If a String End With Specific Text

Sovary May 28, 2022 482
2 minutes read

We have a single sentence how can we check whether the string is end with specific word? in Python we can use endswith() method to check as string have particular suffix value.

The endswith() is built-in string method to check works in the same way as the startswith() method. I have wrote about how to search string prefix in a sentence as well. Following with example below to understand how to check if suffix string is matched.

endswith() -  checks if a string ends with a specific value.

Syntax

str.endswith(suffix, start_index, end_index): boolean

Return Value: Returns boolean. True if str end with specific suffix if not return False.

To understand more clearly, I will explain about index value. In every programming languages, characters in a string have own index value. The index always start with 0 (Zero) located where character is in string. Let's see example below:

String E a t   C a k e
Index 0 1 2 3 4 5 6 7
Position 1 2 3 4 5 6 7 8

You can see example above letter E is start in index 0 and all indexes are 7 but if count as string length there are 8 characters.

Parameters

Parameters Type Description
suffix String , Tuple (Required) String or Tuple data to be check with str
start_index Integer (Optional) Start index to be check string with prefix
end_index Integer (Optional) End index to be check string with prefix

 

 

Examples

Example #1 - Using endswith() with String without optional parameters

text = "Learn quick tutorial and easy"

result = text.endswith("easy")
#True
print(result)

result = text.endswith("Easy")
#False
print(result)

Output:

True
False

Example #2 - Using endswith() with optional parameters

# String length 29 characters
text = "Learn quick tutorial and easy"

# Start search character at index 4 "n quick tutorial and easy"
result = text.endswith("easy",4)
#True
print(result)

# Start search from character at index 25 "Learn quick tutorial and "
# End index 28 "y"
result = text.endswith("eas",25,28)
#True
print(result)

Output:

True
True

 

 

Example #3 - Using endswith() with Tuple parameter

endswith() support with tuple value parameter to search in suffix string. If the string is end with one of tuple item will return true, else return false

# String length 29 characters
text = "Learn quick tutorial and easy"

# Take tuple items("python","easy","tools")search
# If text variable end with these items
result = text.endswith(("python","easy","tools"))
#True
print(result)

# Tuple items is searching in "tutorial and easy"
# If tuple item end with
result = text.endswith(("python","learn","quick"),12)
#False
print(result)

# Tuple items is searching in "Learn quick"
# If tuple item end with
result = text.endswith(("python","easy","quick"),0,11)
#True
print(result)

 Output:

True
False
True

Conclusion

In this tutorial we simply learned how to search string within particular suffix string. The example above hope it will help to handle your task. Hope you understand and learn in this article.

You might Also Like:

 

Python 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him