Skip to main content
 首页 » 编程设计

python之字符串内求值

2025年05月04日18lvdongjie

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)。