字符串拼接的完整代码
时间: 2023-11-28 12:45:11 浏览: 50
以下是Python中字符串拼接的几种方式的完整代码:
1.使用"+"号拼接字符串:
```python
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # 输出:Hello World
```
2.使用join()方法拼接字符串:
```python
str_list = ["Hello", "World"]
result = " ".join(str_list)
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
```
阅读全文