gpt4 book ai didi

python-2.7 - 字符串中 %s 的 Python 3 等价物是什么?

转载 作者:行者123 更新时间:2023-12-03 23:31:52 24 4
gpt4 key购买 nike

我之前在 python 方面取得了一些基本的进步,只不过是命令土地代数计算器来做数学作业,使用用户定义的函数、输入和基本的东西。从那以后,我参加了 codeacademy 的 Python 2 类(class),我发现没有等同于在 PY3 中使用 % 和 %s 的方法。

我已经将范围缩小到与 format() 有某种关系,但这是我在谷歌上所能找到的。

作为初学者,我非常感谢如何将其转换为 Python 3 的淡化解释:

str1 = "Bob,"
str2 = "Marcey."
print "Hello %s hello %s" % (str1, str2)

编辑:另外,我知道 print("Hello "+ str1 + "hello "+ str2) 会起作用。

最佳答案

str.__mod__()继续在 3.x 中工作,但使用 str.format() 执行字符串格式化的新方法描述于 PEP 3101 ,并随后被反向移植到 2.x 的最新版本。

print("Hello %s hello %s" % (str1, str2))

print("Hello {} hello {}".format(str1, str2))

关于python-2.7 - 字符串中 %s 的 Python 3 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26029389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com