Python Check Whether a String Ends with a Specific Character
In Python, we can use the string's endswith() method to determine whether a string ends with a specific character or substring. This method returns a boolean: it returns True; otherwise, returns False。
Example
Define a string
text
=
"Hello, world!"
Check whether the string ends with "world!"
result
=
text.
endswith
(
"world!"
)
Output result
(
result
)
Code analysis:
text = "Hello, world!": Define a string variabletext, whose value is"Hello, world!"。result = text.endswith("world!"): When usingendswith()The method checks whethertextends with"world!"and stores the result in the variableresultit.print(result): Print the value ofresult, that is,TrueorFalse。
Output result:
True
In this example, the string "Hello, world!" Starts with "world!" at the end, therefore endswith() method returns True。
Python3 Examples