# 示例代码:Python字符串连接
# 方法1:使用加号 (+) 连接字符串
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # 输出: Hello World
# 方法2:使用 join() 方法连接字符串
words = ["Python", "is", "awesome"]
sentence = " ".join(words)
print(sentence) # 输出: Python is awesome
# 方法3:使用 f-string (格式化字符串字面量) 连接字符串 (Python 3.6+)
name = "Alice"
greeting = f"Hello, {name}!"
print(greeting) # 输出: Hello, Alice!
# 方法4:使用 % 操作符连接字符串 (较旧的方式)
item = "book"
price = 10
description = "The %s costs %d dollars." % (item, price)
print(description) # 输出: The book costs 10 dollars.
+ 号将两个或多个字符串连接在一起。printf,虽然仍然可用,但推荐使用 f-string 或 format() 方法。上一篇:minio python
下一篇:python 转义字符
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站