为了账号安全,请及时绑定邮箱和手机立即绑定

Python字符串格式:%vs. .format

Python字符串格式:%vs. .format

Python字符串格式:%vs. .formatPython 2.6引入了该str.format()方法,其语法与现有%运算符略有不同。哪种情况更好,哪种情况更好?以下使用每种方法并具有相同的结果,那么有什么区别?#!/usr/bin/pythonsub1 = "python string!"sub2 = "an arg"a = "i am a %s" % sub1b = "i am a {0}".format(sub1)c = "with %(kwarg)s!" % {'kwarg':sub2}d = "with {kwarg}!".format(kwarg=sub2)print a    # "i am a python string!"print b    # "i am a python string!"print c    # "with an arg!"print d    # "with an arg!"此外,何时在Python中发生字符串格式化?例如,如果我的日志记录级别设置为HIGH,我仍然会执行以下%操作吗?如果是这样,有没有办法避免这种情况?log.debug("some debug info: %s" % some_info)
查看完整描述

4 回答

?
炎炎设计

TA贡献1808条经验 获得超4个赞

回答你的第一个问题...... .format在许多方面似乎更复杂。令人讨厌的%是它如何能够采用变量或元组。您认为以下内容始终有效:

"hi there %s" % name

然而,如果name恰好是(1, 2, 3),它会抛出一个TypeError。为了保证它始终打印,您需要这样做

"hi there %s" % (name,)   # supply the single argument as a single-item tuple

这只是丑陋的。.format没有那些问题。同样在你给出的第二个例子中,这个.format例子看起来更清晰。

你为什么不用它?

  • 不知道它(我在读这篇文章之前)

  • 必须与Python 2.5兼容


要回答第二个问题,字符串格式化与任何其他操作同时发生 - 评估字符串格式化表达式时。并且Python不是一种懒惰的语言,在调用函数之前会对表达式求值,所以在你的log.debug例子中,表达式"some debug info: %s"%some_info将首先求值,例如"some debug info: roflcopters are active",然后传递给该字符串log.debug()


查看完整回答
反对 回复 2019-05-28
?
繁星淼淼

TA贡献1775条经验 获得超11个赞

模数运算符(%)不能做的事情,afaik:


tu = (12,45,22222,103,6)

print '{0} {2} {1} {2} {3} {2} {4} {2}'.format(*tu)

结果


12 22222 45 22222 103 22222 6 22222

很有用。


另一点:format()作为一个函数,可以在其他函数中用作参数:


li = [12,45,78,784,2,69,1254,4785,984]

print map('the number is {}'.format,li)   


print


from datetime import datetime,timedelta


once_upon_a_time = datetime(2010, 7, 1, 12, 0, 0)

delta = timedelta(days=13, hours=8,  minutes=20)


gen =(once_upon_a_time +x*delta for x in xrange(20))


print '\n'.join(map('{:%Y-%m-%d %H:%M:%S}'.format, gen))

结果是:


['the number is 12', 'the number is 45', 'the number is 78', 'the number is 784', 'the number is 2', 'the number is 69', 'the number is 1254', 'the number is 4785', 'the number is 984']


2010-07-01 12:00:00

2010-07-14 20:20:00

2010-07-28 04:40:00

2010-08-10 13:00:00

2010-08-23 21:20:00

2010-09-06 05:40:00

2010-09-19 14:00:00

2010-10-02 22:20:00

2010-10-16 06:40:00

2010-10-29 15:00:00

2010-11-11 23:20:00

2010-11-25 07:40:00

2010-12-08 16:00:00

2010-12-22 00:20:00

2011-01-04 08:40:00

2011-01-17 17:00:00

2011-01-31 01:20:00

2011-02-13 09:40:00

2011-02-26 18:00:00

2011-03-12 02:20:00


查看完整回答
反对 回复 2019-05-28
?
万千封印

TA贡献1891条经验 获得超3个赞

假设您正在使用Python的logging模块,您可以将字符串格式化参数作为参数传递给.debug()方法,而不是自己进行格式化:

log.debug("some debug info: %s", some_info)

这避免了格式化,除非记录器实际记录了一些东西。


查看完整回答
反对 回复 2019-05-28
  • 4 回答
  • 0 关注
  • 817 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信