# 字符串拼接操作Python
# 方法1:使用 + 运算符
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # 输出: Hello World
# 方法2:使用 join() 方法
words = ["Hello", "World"]
result = " ".join(words)
print(result) # 输出: Hello World
# 方法3:使用 f-string (格式化字符串)
name = "Alice"
greeting = f"Hello, {name}!"
print(greeting) # 输出: Hello, Alice!
# 方法4:使用 % 操作符
age = 25
message = "I am %d years old." % age
print(message) # 输出: I am 25 years old.
# 方法5:使用 format() 方法
city = "Beijing"
info = "I live in {}".format(city)
print(info) # 输出: I live in Beijing
以上代码展示了在 Python 中进行字符串拼接的几种常见方法。每种方法都有其适用场景,可以根据具体需求选择合适的方式。
上一篇:0基础学python
下一篇:python sep用法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站