您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python是否执行类似于Ruby中的“字符串#{var}”的变量插值?

Python是否执行类似于Ruby中的“字符串#{var}”的变量插值?

Python 3.6+确实具有变量插值-f在您的字符串前添加

f"foo is {bar}"

对于低于此版本的Python版本(Python 2-3.5),您可以str.format用来传递变量:

# Rather than this:
print("foo is #{bar}")

# You would do this:
print("foo is {}".format(bar))

# Or this:
print("foo is {bar}".format(bar=bar))

# Or this:
print("foo is %s" % (bar, ))

# Or even this:
print("foo is %(bar)s" % {"bar": bar})
python 2022/1/1 18:36:42 有423人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶