python 判断某个string是否在string中
时间: 2023-11-25 21:06:12 浏览: 26
可以使用 Python 中的 `in` 关键字来判断一个字符串是否存在于另一个字符串中。示例代码如下:
```python
s1 = "hello world"
s2 = "world"
s3 = "Python"
if s2 in s1:
print(s2, "exists in", s1)
else:
print(s2, "does not exist in", s1)
if s3 in s1:
print(s3, "exists in", s1)
else:
print(s3, "does not exist in", s1)
```
输出结果为:
```
world exists in hello world
Python does not exist in hello world
```
相关问题
python如何判断String A 是否包含 String B
可以使用Python中的in关键字来判断String A是否包含String B,例如:
if "String B" in "String A":
print("String A 包含 String B")
else:
print("String A 不包含 String B")
这样就可以判断String A是否包含String B了。
Python判断一个string是否是两位数字
可以使用以下代码来判断一个string是否是两位数字:
```python
s = '23'
if s.isdigit() and len(s) == 2:
print('是两位数字')
else:
print('不是两位数字')
```
其中,`isdigit()`方法用于判断字符串是否由数字组成,如果是则返回True,否则返回False。`len()`函数用于获取字符串的长度。如果字符串由数字组成且长度为2,则判定为两位数字。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)