python - 字符串内求值

标签 python

Python 中是否有类似 Ruby 的 "Hello #{userName Function()}" 的东西?

最佳答案

在 Python 中,您将使用字符串插值

"Hello %s" % user_name_function()

或字符串格式化

"Hello {0}".format(user_name_function())

后者在 Python 2.6 或更高版本中可用。

另请注意,按照惯例,您不使用 CamelCase 作为 Python 中的函数名称(CamelCase 仅用于类名——参见 PEP 8)。

关于python - 字符串内求值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5282503/

相关文章:

python - 将内核更改为另一个版本的 python,而不是 Mac OSX 中的系统 Python

python 使用 groupby 滚动累积返回

python - 使用生成器循环遍历列表中的数字

Python:填写数据框中缺少的日期时间值并向前填写?

python - Python 2.7 和 Python 3.3 之间的函数类型检查

python - TensorFlow 中的变换矩阵

python - 如何以编程方式将绑定(bind)添加到 Python 中的当前类范围?

python - 通过查找字符 "in"字符串来切片 pandas 数据帧

Python 数据库 sqlite3

python - Matplotlib 使中心圆透明