Python String endswith() Method
The Python endswith() method is a string method that returns True if the string ends with the specified suffix, otherwise False. The method can also take optional parameters to specify the start and end positions of the range to check within the string.
Here are some examples of using the endswith() method in Python:
To check if a string ends with a punctuation mark:
txt = "Hello, welcome to my world."
x = txt.endswith(".")
print(x)
Output: True
To check if a string ends with a certain word:
str = "Hello!Welcome to Tutorialspoint."
suffix = "oint"
x = str.endswith(suffix)
print(x)
Output: True
To check if a string ends with a substring within a specified range:
text = "Python is easy to learn."
result = text.endswith('is', 0, 8)
print(result)
Output: True
To check if a string ends with any of multiple suffixes:
text = "geeks for geeks."
suffixes = (".", "?", "!")
result = text.endswith(suffixes)
print(result)
Output: True
```[assistant](#suggestions)
```json
{"suggestedUserResponses": ["What are some use cases for endswith()?", "How can I check if a string starts with a certain prefix?", "Show me more examples of endswith().", "What happens if I pass an empty suffix?"]}
If you have any questions about this code, you can drop a line in comment.
Comments
Post a Comment