python 两个字符串是否有相同字符
时间: 2024-02-05 16:43:32 浏览: 24
可以使用set()函数来判断两个字符串是否有相同的字符,代码如下:
```python
str1 = "hello"
str2 = "world"
if set(str1) & set(str2):
print("两个字符串有相同的字符")
else:
print("两个字符串没有相同的字符")
```
输出结果为:
```
两个字符串有相同的字符
```
相关问题
python 两个字符串比较是否相同
在 Python 中,可以使用 `==` 运算符来比较两个字符串是否相同。例如:
```python
s1 = "hello"
s2 = "world"
if s1 == s2:
print("The strings are equal")
else:
print("The strings are not equal")
```
如果 `s1` 和 `s2` 字符串相同,则输出 `The strings are equal`,否则输出 `The strings are not equal`。
python判断两个字符串是否相同
可以使用 == 运算符来判断两个字符串是否相同,例如:
```
str1 = "hello"
str2 = "world"
if str1 == str2:
print("两个字符串相同")
else:
print("两个字符串不相同")
```
如果 str1 和 str2 相同,输出结果为 "两个字符串不相同",否则输出结果为 "两个字符串不相同"。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)