Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Check Whether a String Ends with a Specific Character

Python Check Whether a String Ends with a Specific Character

Document Object Reference Manual Python3 Examples

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

print

(

result

)

Code analysis:

Output result:

True

In this example, the string "Hello, world!" Starts with "world!" at the end, therefore endswith() method returns True。

Document Object Reference Manual Python3 Examples