扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

在Python中怎么向字符串添加字符

扬州沐宇科技
2024-06-05 10:19:17
python

要向字符串添加字符,可以使用字符串拼接或者字符串格式化的方法。

  1. 字符串拼接:
s = "Hello"
s += " World"
print(s)  # 输出:Hello World
  1. 字符串格式化:
s = "Hello"
s = "{} World".format(s)
print(s)  # 输出:Hello World
  1. 使用f-string(Python 3.6及以上版本):
s = "Hello"
s = f"{s} World"
print(s)  # 输出:Hello World

扫码添加客服微信