syntax – Whats the u prefix in a Python string?

syntax – Whats the u prefix in a Python string?

Youre right, see 3.1.3. Unicode Strings.

Its been the syntax since Python 2.0.

Python 3 made them redundant, as the default string type is Unicode. Versions 3.0 through 3.2 removed them, but they were re-added in 3.3+ for compatibility with Python 2 to aide the 2 to 3 transition.

The u in uSome String means that your string is a Unicode string.

Q: Im in a terrible, awful hurry and I landed here from Google Search. Im trying to write this data to a file, Im getting an error, and I need the dead simplest, probably flawed, solution this second.

A: You should really read Joels Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) essay on character sets.

Q: sry no time code pls

A: Fine. try str(Some String) or Some String.encode(ascii, ignore). But you should really read some of the answers and discussion on Converting a Unicode string and this excellent, excellent, primer on character encoding.

syntax – Whats the u prefix in a Python string?

My guess is that it indicates Unicode, is it correct?

Yes.

If so, since when is it available?

Python 2.x.

In Python 3.x the strings use Unicode by default and theres no need for the u prefix. Note: in Python 3.0-3.2, the u is a syntax error. In Python 3.3+ its legal again to make it easier to write 2/3 compatible apps.

Leave a Reply

Your email address will not be published. Required fields are marked *