Python String startswith() and endswith() Guide

You can use the startswith() and endswith() methods in Python to check if a string starts or ends with a specific substring.

For example, to check if a string str starts with a substring prefix, you can use the following code:

if str.startswith(prefix):
    print("字符串以特定子字符串开头")
else:
    print("字符串不以特定子字符串开头")

Similarly, to check if a string “str” ends with a substring “suffix”, you can use the following code:

if str.endswith(suffix):
    print("字符串以特定子字符串结尾")
else:
    print("字符串不以特定子字符串结尾")
bannerAds