在Python中,添加字符串可以通过以下几种方法实现:
1. 使用加号(`+`)操作符:
string1 = "hello"
string2 = "world"
new_string = string1 + " " + string2
print(new_string) 输出:hello world
2. 使用字符串的`join()`方法:
string1 = "hello"
string2 = "world"
new_string = "".join([string1, " ", string2])
print(new_string) 输出:hello world
`join()`方法可以将一个可迭代对象(如列表、元组等)中的元素连接成一个新的字符串,元素之间用指定的分隔符(这里是空格)分隔。