面试题答案
一键面试- 字符串格式化(旧风格):使用
%
操作符进行字符串格式化。 str.format()
方法:一种更灵活的字符串格式化方式。f-string
(Python 3.6+):简洁直观的字符串格式化方式。
以下是使用f-string
实现简单模板填充的示例:
name = "张三"
age = 25
template = f"姓名:{name},年龄:{age}"
print(template)
%
操作符进行字符串格式化。str.format()
方法:一种更灵活的字符串格式化方式。f-string
(Python 3.6+):简洁直观的字符串格式化方式。以下是使用f-string
实现简单模板填充的示例:
name = "张三"
age = 25
template = f"姓名:{name},年龄:{age}"
print(template)