Files
Python_CookBook_repo/2.字符串和文本/2.字符串首尾文字匹配.py

22 lines
822 B
Python
Raw Permalink Normal View History

2025-09-10 16:12:45 +08:00
from numpy.core.defchararray import startswith
if __name__ == '__main__':
str = "https://www.baidu.com"
str2 = "http://www.baidu.cn"
str3 = "suka"
# 如果想要在字符串首进行匹配,可以使用:
is_start_with_https = str.startswith("https")
print(is_start_with_https)
# 这会输出一个布尔值,
## 比如上面匹配成功就会输出True
# 同理可以设置endswith
is_end_with_dotcom = str.endswith(".com")
print(is_end_with_dotcom)
# 如果想要匹配多种开头和结尾,可以将函数输入改成元组()
is_url = [url for url in [str, str2, str3] if url.startswith(('http', 'https'))]
print(is_url)
# 当然,复杂的操作还请使用正则表达式进行匹配,不过简单的检查用这个方法那是又快又好