Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any good reason not to use unicode as opposed to string?

Tags:

python

unicode

Many problems I've ran into in Python have been related to not having something in Unicode. Is there any good reason to not use Unicode by default? I understand needing to translate something in ASCII, but it seems to be the exception and not the rule.

I know Python 3 uses Unicode for all strings. Should this encourage me as a developer to unicode() all my strings?

like image 593
Naftuli Kay Avatar asked Nov 28 '11 21:11

Naftuli Kay


2 Answers

Generally, I'm going to say "no" there's not a good reason to use string over unicode. Remember, as well, that you don't have to call unicode() to create a unicode string, you can do so by prefixing the string with a lowercase u like u"this is a unicode string".

like image 77
g.d.d.c Avatar answered Sep 19 '22 11:09

g.d.d.c


In Python 2.x:

  • A str object is basically just a sequence of bytes.
  • A unicode object is a sequence of characters.

Knowing this, it should be easy to choose the correct type:

  • If you want a string of characters use unicode.
  • If you want an string encoded as bytes use str (in many other languages you'd use byte[] here).

In Python 3.x the type str is a string of characters, just as you would expect. You can use bytes if you want a sequence of bytes.

like image 45
Mark Byers Avatar answered Sep 19 '22 11:09

Mark Byers