python - 如何将变量存储在多行字符串变量中?
问题描述
如何将变量存储在多行字符串中?
health = 100
stats = """
Your health is: [health]
"""
print(stats)
它指示[health]
的位置将替换为变量health
( 100
)。
解决方案
使用f
-strings - 格式化字符串的最简单实用 ( pythonic ) 方式。
health = 100
stats = f"""
Your health is: {health}
"""
print(stats)
#
# Your health is: 100
#
最有趣的部分是f
-strings 还支持大括号内的任何 Python 表达式。
health = 100
stats = f"""
Your health is: {health * health}
"""
print(stats)
#
# Your health is: 10000
#
推荐阅读
- git - GitHub Actions - 将公共回购克隆到我的私人回购?
- firebase - 在 Flutter Firebase 存储中显示我的所有文件夹
- foreign-keys - 无法在 mysql Workbench 中添加外键用户表
- android - 分享android原始资源
- jmeter - 如何使用 MS 帐户在 30 分钟内模拟 5000 个用户登录
- python - 熊猫矢量化的问题
- sql - 如果相同的值,则从另一个表更新 Postgres 表的列值,否则为不匹配的值插入新行
- ruby-on-rails - Rails 6.0 Seed.rb 文件在运行 rails db:seed 后未保存到 sqlite3 db
- css - Nativescript-Vue 如何让不影响孩子的页面组件透明化
- apache-spark - 左连接错误:org.apache.spark.sql.AnalysisException:检测到逻辑计划之间左外连接的隐式笛卡尔积