共計(jì) 669 個(gè)字符,預(yù)計(jì)需要花費(fèi) 2 分鐘才能閱讀完成。
- 使用加號(hào)運(yùn)算符 “+”
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # Output: Hello World
- 使用逗號(hào)分隔的多個(gè)字符串
str1 = "Hello"
str2 = "World"
print(str1, str2) # Output: Hello World
- 使用字符串的 join() 方法
str1 = "Hello"
str2 = "World"
result = " ".join([str1, str2])
print(result) # Output: Hello World
- 使用 f-string 格式化字符串
str1 = "Hello"
str2 = "World"
result = f"{str1} {str2}"
print(result) # Output: Hello World
- 使用字符串的 format() 方法
str1 = "Hello"
str2 = "World"
result = "{} {}".format(str1, str2)
print(result) # Output: Hello World
- 使用字符串的 % 格式化
str1 = "Hello"
str2 = "World"
result = "%s %s" % (str1, str2)
print(result) # Output: Hello World
- 使用列表推導(dǎo)式和字符串的 join() 方法
str1 = "Hello"
str2 = "World"
result = " ".join([word for word in [str1, str2]])
print(result) # Output: Hello World
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完