【问题标题】:What is the difference between the f" and the format() method in python [duplicate]python中的f"和format()方法有什么区别[重复]
【发布时间】:2020-12-05 01:08:56
【问题描述】:

所以我在 python 中尝试了格式字符串方法,但它们之间有什么区别?哪种方法最适合我?

示例 1: name = 'Dash'

print(f'Hello {name}!')

示例 2: name = 'Dash'

print('Hello {}!'.format(name))

【问题讨论】:

  • 如您所见,没有区别。两者都是字符串格式化选项,新引入了 f-strings。

标签: python string methods


【解决方案1】:

Print f 只是一种将变量插入字符串的更新且更简单的方法。我认为它出现在 python 3.6 中。两者都做同样的事情

【讨论】:

    【解决方案2】:

    实际上两者都做同样的事情。 您提到的 f 是一个 f 字符串,可从 python 3.6 获得

    【讨论】:

      猜你喜欢
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      相关资源
      最近更新 更多