Python怎么拼接字符串
时间: 2023-11-05 16:09:08 浏览: 65
python实现字符串和数字拼接
Python拼接字符串有多种方式,以下是其中几种常见的方法:
1. 使用加号(+)运算符:
```python
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # 输出: Hello World
```
2. 使用join()方法:
```python
str1 = "Hello"
str2 = "World"
result = " ".join([str1, str2])
print(result) # 输出: Hello World
```
3. 使用格式化字符串(f-string):
```python
str1 = "Hello"
str2 = "World"
result = f"{str1} {str2}"
print(result) # 输出: Hello World
```
4. 使用format()方法:
```python
str1 = "Hello"
str2 = "World"
result = "{} {}".format(str1, str2)
print(result) # 输出: Hello World
```
5. 使用%运算符:
```python
str1 = "Hello"
str2 = "World"
result = "%s %s" % (str1, str2)
print(result) # 输出: Hello World
```
阅读全文