# 示例代码:Python 字符串合并
# 方法1:使用加号 (+) 连接字符串
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # 输出: Hello World
# 方法2:使用 join() 方法连接字符串
words = ["Python", "is", "awesome"]
result = " ".join(words)
print(result) # 输出: Python is awesome
# 方法3:使用 f-string (格式化字符串字面量) (Python 3.6+)
name = "Alice"
greeting = f"Hello, {name}!"
print(greeting) # 输出: Hello, Alice!
# 方法4:使用 % 操作符 (旧式格式化)
age = 30
message = "I am %d years old." % age
print(message) # 输出: I am 30 years old.
# 方法5:使用 format() 方法
template = "My name is {} and I am {} years old."
info = template.format("Bob", 25)
print(info) # 输出: My name is Bob and I am 25 years old.
format()
。下一篇:python len函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站