2025-09-10:仓库迁移
This commit is contained in:
21
2.字符串和文本/2.字符串首尾文字匹配.py
Normal file
21
2.字符串和文本/2.字符串首尾文字匹配.py
Normal file
@@ -0,0 +1,21 @@
|
||||
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)
|
||||
|
||||
# 当然,复杂的操作还请使用正则表达式进行匹配,不过简单的检查用这个方法那是又快又好
|
Reference in New Issue
Block a user