文章内容
2023/3/15 15:27:47,作 者: 黄兵
Python endswith() 函数
下面有三个 hostname:
234.52.138.34.bc.googleusercontent.com158.212.138.34.bc.googleusercontent.com
169.185.237.35.bc.googleusercontent.com
我们如何使用 Python 如何判断是否包含:bc.googleusercontent.com
可以使用 Python 中的字符串方法 endswith()
来判断一个字符串是否以指定的后缀结尾。以下是使用 endswith()
方法来判断这三个 hostname 是否包含 "bc.googleusercontent.com" 的示例代码:
hostname1 = "158.212.138.34.bc.googleusercontent.com" hostname2 = "234.52.138.34.bc.googleusercontent.com" hostname3 = "169.185.237.35.bc.googleusercontent.com" if hostname1.endswith("bc.googleusercontent.com"): print("hostname1 包含 bc.googleusercontent.com") if hostname2.endswith("bc.googleusercontent.com"): print("hostname2 包含 bc.googleusercontent.com") if hostname3.endswith("bc.googleusercontent.com"): print("hostname3 包含 bc.googleusercontent.com")
下面是结果:
hostname1 包含 bc.googleusercontent.com hostname2 包含 bc.googleusercontent.com hostname3 包含 bc.googleusercontent.com
可以看到,所有的三个 hostname 都包含 "bc.googleusercontent.com" 这个后缀。
评论列表