Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

python - 字符串格式:%与.format(String formatting: % vs. .format)

Python 2.6 introduced the str.format() method with a slightly different syntax from the existing % operator.

(Python 2.6引入了str.format()方法,其语法与现有的%运算符略有不同。)

Which is better and for what situations?

(哪个更好,什么情况下适合?)

  1. The following uses each method and has the same outcome, so what is the difference?

    (以下使用每种方法并具有相同的结果,那么有什么区别?)

     #!/usr/bin/python sub1 = "python string!" sub2 = "an arg" a = "i am a %s" % sub1 b = "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!" 
  2. Furthermore when does string formatting occur in Python?

    (此外,何时在Python中进行字符串格式化?)

    For example, if my logging level is set to HIGH will I still take a hit for performing the following % operation?

    (例如,如果我的日志记录级别设置为“高”,执行以下%操作是否还会受到影响?)

    And if so, is there a way to avoid this?

    (如果是这样,有办法避免这种情况吗?)

     log.debug("some debug info: %s" % some_info) 
  ask by NorthIsUp translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To answer your first question... .format just seems more sophisticated in many ways.

(要回答您的第一个问题... .format在许多方面似乎都更加复杂。)

An annoying thing about % is also how it can either take a variable or a tuple.

(关于%一个烦人的事情是它如何可以接受变量或元组。)

You'd think the following would always work:

(您会认为以下各项将始终有效:)

"hi there %s" % name

yet, if name happens to be (1, 2, 3) , it will throw a TypeError .

(但是,如果name恰好是(1, 2, 3) ,它将抛出TypeError 。)

To guarantee that it always prints, you'd need to do

(为了确保它始终打印,您需要执行)

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

which is just ugly.

(真丑。)

.format doesn't have those issues.

(.format没有那些问题。)

Also in the second example you gave, the .format example is much cleaner looking.

(同样在您给出的第二个示例中, .format示例看起来更.format 。)

Why would you not use it?

(为什么不使用它?)

  • not knowing about it (me before reading this)

    (不知道(我在阅读本文之前))

  • having to be compatible with Python 2.5

    (必须与Python 2.5兼容)


To answer your second question, string formatting happens at the same time as any other operation - when the string formatting expression is evaluated.

(为了回答您的第二个问题,字符串格式化与其他任何操作都同时发生-计算字符串格式化表达式时。)

And Python, not being a lazy language, evaluates expressions before calling functions, so in your log.debug example, the expression "some debug info: %s"%some_info will first evaluate to, eg "some debug info: roflcopters are active" , then that string will be passed to log.debug() .

(而且,Python不是一种惰性语言,它会在调用函数之前先评估表达式,因此在您的log.debug示例中,表达式"some debug info: %s"%some_info首先会评估为例如"some debug info: roflcopters are active" ,那么该字符串将传递给log.debug() 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...