python 判断某个字符串是否在另一个字符串里面
时间: 2023-10-02 19:12:13 浏览: 60
可以使用 Python 的 `in` 关键字来判断一个字符串是否在另一个字符串中。例如:
```python
string1 = "hello world"
string2 = "world"
if string2 in string1:
print("String2 is in String1")
else:
print("String2 is not in String1")
```
输出结果为:
```
String2 is in String1
```
上面的代码中,我们先定义了两个字符串 `string1` 和 `string2`,然后使用 `in` 关键字来判断 `string2` 是否在 `string1` 中。如果在,就输出 `String2 is in String1`,否则输出 `String2 is not in String1`。
相关问题
python 判断一个字符串是否在另一个字符串中的代码
可以使用 `in` 关键字来判断一个字符串是否在另一个字符串中。例如:
```python
s1 = "hello world"
s2 = "world"
if s2 in s1:
print("s2 is in s1")
else:
print("s2 is not in s1")
```
输出结果为:
```
s2 is in s1
```
如果要忽略大小写来比较字符串,可以先将两个字符串转换为同一种大小写,再进行比较。例如:
```python
s1 = "Hello World"
s2 = "world"
if s2.lower() in s1.lower():
print("s2 is in s1")
else:
print("s2 is not in s1")
```
输出结果仍为:
```
s2 is in s1
```
python判断字符串是否在另一个字符串里 忽略大小写
可以使用字符串的 `lower()` 方法将字符串转换为小写,然后再进行判断。
示例代码:
```python
string1 = "Hello World"
string2 = "world"
if string2.lower() in string1.lower():
print("Yes")
else:
print("No")
```
输出结果为 `Yes`,因为字符串 `"world"` 在字符串 `"Hello World"` 中出现了,并且忽略了大小写。
相关推荐
![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)