python字符串组合输出

2024-01-16 12:37:17

在Python中,可以使用"+"运算符将两个字符串进行组合输出。例如:

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

输出结果为:

HelloWorld

另外,还可以使用字符串的格式化方法`format()`来组合输出字符串。例如:

name = "Zeus"
age = 25
result = "My name is {} and I am {} years old.".format(name, age)
print(result)

输出结果为:

My name is Zeus and I am 25 years old.

还可以使用f-string来进行字符串组合输出。例如:

name = "Zeus"
age = 25
result = f"My name is {name} and I am {age} years old."
print(result)

输出结果为:

My name is Zeus and I am 25 years old.