我是Python的新用户,在阅读代码时我不知道某些部分。所以我在这里问。python string u“%(word)s”
cmd = u"sudo umount %(mountpoint)s >>%(log)s 2>&1"
我知道%(word)
用来替换的单词后使用,如cmd % {'word':'new word'}
,但我不下来,为什么有一个尾随s
。我不知道字符串开头的'u'意思。
我是Python的新用户,在阅读代码时我不知道某些部分。所以我在这里问。python string u“%(word)s”
cmd = u"sudo umount %(mountpoint)s >>%(log)s 2>&1"
我知道%(word)
用来替换的单词后使用,如cmd % {'word':'new word'}
,但我不下来,为什么有一个尾随s
。我不知道字符串开头的'u'意思。
u
意味着它是Unicode字符串。s
代表字符串。开头u
的意思是“Unicode”(即它是一个Unicode字符串)。每个格式代码后的s
意味着将该值解释为字符串,而不是d
的十进制等。
有关所有详细信息,请参见http://docs.python.org/library/stdtypes.html#string-formatting-operations上的文档。
字符串开头的u表示它是一个unicode字符串。
结尾的s意味着格式的部分应该是一个字符串。如果这是一个一个d这将是一个整数,并且,如果它是一个f这将是一个浮子
s
是一个转换说明符,意思是将其打印为字符串。
参见string formatting,点3:
转换标志(可选),从而影响某些转换类型的结果。
前导u
表示它是一个unicode
对象,而不是str
。
请参阅Unicode in Python, Completely Demystified了解两者之间差异的详细概述。