python怎么連接兩個(gè)字符串并輸出

小億
163
2024-05-08 10:34:45

要連接兩個(gè)字符串并輸出,可以使用加號(hào) + 運(yùn)算符或者使用字符串的 join() 方法。

使用加號(hào)運(yùn)算符:

str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result)

使用join()方法:

str1 = "Hello"
str2 = "World"
result = " ".join([str1, str2])
print(result)

兩種方法都會(huì)輸出:

Hello World

0