With
print(" {:d}). {:s} ({:d})".format(i, account, num_char))
I get the error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 4: ordinal not in range(128)
but when I change it to:
print " %d). %s (%d)" % (i, account, num_char)
then there are no problem and output is identical with both prints.
So what is wrong in the first expression and why does it work in the second?
In the first example, you are calling the format
method of str
object passing unicode
arguments. This causes an error. You should use
print(u" {:d}). {:s} ({:d})".format(i, account, num_char))
instead.
In the second one, you are using the %
operator which automatically returns unicode
when either format or object is unicode
. From the docs:
- If the object or format provided is a unicode string, the resulting string will also be unicode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With