python 判断一个字符串是否在另一个字符串中的代码
时间: 2023-12-31 18:06:32 浏览: 159
可以使用 `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
```
阅读全文