3

Using the Python module unicode-nazi to detect unicode issues, I am running into this warning:

/home/dotancohen/unicode-test.py:51: UnicodeWarning: Implicit conversion of unicode to str
print("Here is a phrase: " + str(phrase))

Since phrase is being explicitly cast to string, where is the implicit conversion? Surely "Here is a phrase: " is a string, as it is not preceded by u.

dda
6,2212 gold badges27 silver badges37 bronze badges
asked Jun 11, 2013 at 13:44
2
  • I don't know if that's what it complains about, but it sure wouldn't hurt to explicitly encode... Commented Jun 11, 2013 at 13:46
  • phrase.encode('1252') Commented Jun 11, 2013 at 13:47

1 Answer 1

6

You need to encode the phrase unicode value explicitly:

print("Here is a phrase: " + phrase.encode('some_codec'))

str() on a unicode value implicitly encodes that value, using the default codec (ASCII on Python 2).

answered Jun 11, 2013 at 13:46
Sign up to request clarification or add additional context in comments.

1 Comment

I see! The warning message should probably say "Implicit encoding of unicode to some_codec in conversion to str". Thank you!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.