Dea*_*ean 159 python string syntax
我有一个看起来像的字符串,'%s in %s'我想知道如何分隔参数,使它们是两个不同的%s.我的思绪来自Java想出了这个:
'%s in %s' % unicode(self.author), unicode(self.publication)
Run Code Online (Sandbox Code Playgroud)
但这不起作用,所以它在Python中看起来如何?
Mar*_*ers 178
Mark Cidade的回答是对的 - 你需要提供一个元组.
但是从Python 2.6开始,您可以使用format而不是%:
'{0} in {1}'.format(unicode(self.author,'utf-8'), unicode(self.publication,'utf-8'))
Run Code Online (Sandbox Code Playgroud)
%不再鼓励使用格式化字符串.
这种字符串格式化方法是Python 3.0中的新标准,应该优先于新代码中字符串格式化操作中描述的%格式.
Mar*_*ade 113
如果你使用多个参数,它必须是一个元组(注意额外的括号):
'%s in %s' % (unicode(self.author), unicode(self.publication))
Run Code Online (Sandbox Code Playgroud)
正如EOL指出的那样,该unicode()函数通常假设ascii编码为默认值,因此如果您有非ASCII字符,则显式传递编码会更安全:
'%s in %s' % (unicode(self.author,'utf-8'), unicode(self.publication('utf-8')))
Run Code Online (Sandbox Code Playgroud)
从Python 3.0开始,最好使用str.format()语法:
'{0} in {1}'.format(unicode(self.author,'utf-8'),unicode(self.publication,'utf-8'))
Run Code Online (Sandbox Code Playgroud)
pol*_*nts 54
format以下内容摘自文档:
给定
format % values,%转换规范format被替换为零个或多个元素values.效果类似于sprintf()C语言中的使用.如果
format需要单个参数,则值可以是单个非元组对象.否则,值必须是具有format字符串指定的项目数的元组,或者是单个映射对象(例如,字典).
str.format代替%%运营商的新选择是使用str.format.以下是文档的摘录:
str.format(*args, **kwargs)执行字符串格式化操作.调用此方法的字符串可以包含由大括号分隔的文字文本或替换字段
{}.每个替换字段都包含位置参数的数字索引或关键字参数的名称.返回字符串的副本,其中每个替换字段都替换为相应参数的字符串值.此方法是Python 3.0中的新标准,应该优先于
%格式化.
以下是一些用法示例:
>>> '%s for %s' % ("tit", "tat")
tit for tat
>>> '{} and {}'.format("chicken", "waffles")
chicken and waffles
>>> '%(last)s, %(first)s %(last)s' % {'first': "James", 'last': "Bond"}
Bond, James Bond
>>> '{last}, {first} {last}'.format(first="James", last="Bond")
Bond, James Bond
Run Code Online (Sandbox Code Playgroud)
Bah*_*mir 10
您必须将值放入括号中:
'%s in %s' % (unicode(self.author), unicode(self.publication))
Run Code Online (Sandbox Code Playgroud)
在这里,第一个%s的unicode(self.author)将被放置.而对于第二个%s,unicode(self.publication)将使用.
注意:你应该赞成
string formatting使用%符号.更多信息在这里
到目前为止发布的一些答案存在严重问题:unicode()来自默认编码的解码,通常是ASCII; 事实上,unicode()试图通过将它们转换为字符来对其给出的字节进行"感知".因此,以下代码(基本上是以前的答案推荐的代码)在我的机器上失败:
# -*- coding: utf-8 -*-
author = 'éric'
print '{0}'.format(unicode(author))
Run Code Online (Sandbox Code Playgroud)
得到:
Traceback (most recent call last):
File "test.py", line 3, in <module>
print '{0}'.format(unicode(author))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
失败的原因是author不包含ASCII字节(即[0; 127]中的值),unicode()默认情况下从ASCII解码(在许多机器上).
一个强大的解决方案是明确给出您的字段中使用的编码; 以UTF-8为例:
u'{0} in {1}'.format(unicode(self.author, 'utf-8'), unicode(self.publication, 'utf-8'))
Run Code Online (Sandbox Code Playgroud)
(或者没有初始值u,具体取决于您是否需要Unicode结果或字节字符串).
此时,可能需要考虑将author和publication字段设置为Unicode字符串,而不是在格式化期间解码它们.
为了完整起见,在PEP-498中引入了 python 3.6 f-string 。这些字符串使
使用最少的语法将表达式嵌入字符串文字中。
这意味着对于您的示例,您还可以使用:
f'{self.author} in {self.publication}'
Run Code Online (Sandbox Code Playgroud)
对于python2,您也可以这样做
'%(author)s in %(publication)s'%{'author':unicode(self.author),
'publication':unicode(self.publication)}
Run Code Online (Sandbox Code Playgroud)
如果你有很多要替代的论据(特别是如果你正在进行国际化),这很方便
Python2.6以上支持 .format()
'{author} in {publication}'.format(author=self.author,
publication=self.publication)
Run Code Online (Sandbox Code Playgroud)
您也可以通过以下方式使用干净和简单(但错了!因为您应该format像 Mark Byers 所说的那样使用):
print 'This is my %s formatted with %d arguments' % ('string', 2)
Run Code Online (Sandbox Code Playgroud)