python 寻找字符串里是否有指定字符串
时间: 2023-10-05 15:14:53 浏览: 41
可以使用 Python 的 in 关键字来判断一个字符串是否包含另一个字符串。例如:
```python
string1 = "hello world"
string2 = "world"
if string2 in string1:
print("string2 is found in string1")
else:
print("string2 is not found in string1")
```
输出结果为:
```
string2 is found in string1
```
如果要判断字符串里是否有多个指定字符串,可以使用多个 in 关键字,例如:
```python
string1 = "hello world"
string2 = "world"
string3 = "python"
if string2 in string1 and string3 in string1:
print("string2 and string3 are both found in string1")
else:
print("string2 and/or string3 are not found in string1")
```
输出结果为:
```
string2 and/or string3 are not found in string1
```
相关问题
python 判断字符串数据里是否有包含指定的关键词
可以使用 in 关键字来判断字符串数据里是否包含指定的关键词,例如:
```python
str = "这是一个包含关键词的字符串"
if "关键词" in str:
print("字符串中包含关键词")
else:
print("字符串中不包含关键词")
```
输出结果为:字符串中包含关键词。
python查找字符串里有多少个子串
可以使用count()方法来查找字符串里有多少个子串,该方法返回字符串中指定子串出现的次数。
例如,在以下字符串中查找子串"an"出现的次数:
```
str = "An apple a day keeps the doctor away"
print(str.count("an"))
```
输出结果为:
```
2
```
说明子串"an"在原字符串中出现了2次。
相关推荐
![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_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)
![](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)