简体   繁体   English

python string u“%(word)s”

[英]python string u“%(word)s”

I'm a new user of Python, and I don't know some parts when I read the code. 我是Python的新用户,在阅读代码时我不知道某些部分。 So I asked here. 所以我在这里问。

cmd = u"sudo umount %(mountpoint)s >>%(log)s 2>&1"

I know that %(word) is used to replace the word latter use such as cmd % {'word':'new word'} , but I don't down why there is a trailing s . 我知道%(word)用于替换后一个使用的单词,例如cmd % {'word':'new word'} ,但我不知道为什么有一个尾随s and I don't know the 'u' meaning at the beginning of the string. 我不知道字符串开头的'u'意思。

The beginning u means "Unicode" (ie it's a Unicode string). 开头u意思是“Unicode”(即它是一个Unicode字符串)。 The s after each formatting code means to interpret the value as a string, as opposed to d for decimal, etc. 每个格式化代码后面的s表示将值解释为字符串,而不是d表示十进制,等等。

See the documentation at http://docs.python.org/library/stdtypes.html#string-formatting-operations for all the details. 有关所有详细信息,请参阅http://docs.python.org/library/stdtypes.html#string-formatting-operations上的文档。

The u at the beginning of the string means that it's a unicode string. 字符串开头的u表示它是一个unicode字符串。

The trailing s means that that part of the formatting should be a string. 尾随s意味着格式化的那部分应该是一个字符串。 If it was aad it would be an integer, and if it was an f it would be a float 如果它是aad它将是一个整数,如果它是f它将是一个浮点数

  1. u means it is a unicode string. u意思是它是一个unicode字符串。
  2. s stands for string. s代表字符串。

s is a conversion specifier, meaning print it as a string. s是转换说明符,意味着将其打印为字符串。

See string formatting , point 3: 请参阅字符串格式 ,第3点:

Conversion flags (optional), which affect the result of some conversion types. 转换标志(可选),它会影响某些转换类型的结果。

The leading u means it is a unicode object, not a str . 领先的u意味着它是一个unicode对象,而不是str

See Unicode in Python, Completely Demystified for a great overview of the difference between the two. 请参阅Python中的Unicode,完全揭秘,以便对两者之间的差异进行全面概述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM