Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python string u"%(word)s"

I'm a new user of Python, and I don't know some parts when I read the code. 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. and I don't know the 'u' meaning at the beginning of the string.

like image 290
mike Avatar asked Feb 16 '26 18:02

mike


2 Answers

The beginning u means "Unicode" (i.e. it's a Unicode string). The s after each formatting code means to interpret the value as a string, as opposed to d for decimal, etc.

See the documentation at http://docs.python.org/library/stdtypes.html#string-formatting-operations for all the details.

like image 98
Gabe Avatar answered Feb 19 '26 06:02

Gabe


The u at the beginning of the string means that it's a unicode string.

The trailing s means that that part of the formatting should be a string. If it was a a d it would be an integer, and if it was an f it would be a float

like image 22
Rafe Kettler Avatar answered Feb 19 '26 07:02

Rafe Kettler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!