python 拼接两个字符串
时间: 2023-10-04 19:11:35 浏览: 68
在 Python 中,可以通过加号 (+) 拼接两个字符串。例如:
```
str1 = "Hello"
str2 = "world"
result = str1 + " " + str2
print(result)
```
输出结果为:
```
Hello world
```
除了加号,还可以使用 join() 方法来拼接多个字符串。例如:
```
str_list = ["Hello", "world"]
result = " ".join(str_list)
print(result)
```
输出结果同样为:
```
Hello world
```
相关问题
如何利用Python拼接两个字符串
要判断两个字符串是否相等,可以使用 == 运算符。例如:
```
s1 = 'hello'
s2 = 'world'
if s1 == s2:
print('s1 和 s2 相等')
else:
print('s1 和 s2 不相等')
```
在此例中,s1 和 s2 不相等,因此输出结果为 's1 和 s2 不相等'。
python中两个字符串拼接
在 Python 中,可以使用 "+" 操作符将两个字符串拼接在一起。例如:
```python
string1 = "Hello"
string2 = "world"
result = string1 + string2
print(result) # 输出:Hello world
```
另外,还可以使用字符串的 join() 方法将多个字符串拼接在一起。例如:
```python
strings = ["Hello", "world"]
result = "".join(strings)
print(result) # 输出:Hello world
```
这里的 `""` 是用来指定连接字符串的分隔符,如果不需要分隔符,可以传入空字符串。