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.
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 | 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 |
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
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
As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him