python如何把两个字符串合成一个

2023-07-30 字符串 两个 合成

可以使用字符串拼接(+号)或字符串格式化来将两个字符串合成一个。

使用字符串拼接:

str1 = "pidancode.com"
str2 = "皮蛋编程"
result = str1 + str2
print(result)  # 输出:pidancode.com皮蛋编程

使用字符串格式化:

str1 = "pidancode.com"
str2 = "皮蛋编程"
result = "{}{}".format(str1, str2)
print(result)  # 输出:pidancode.com皮蛋编程

或者使用f-string:

str1 = "pidancode.com"
str2 = "皮蛋编程"
result = f"{str1}{str2}"
print(result)  # 输出:pidancode.com皮蛋编程

相关文章