Imran Azad
Imran Azad

Reputation: 1404

Difference between binary string, byte string, unicode string and an ordinary string (str)

I'm a little confused. In Python what is the difference between a binary string, byte string, unicode string and a plain old string (str)? I'm using Python 2.6.

Upvotes: 17

Views: 7117

Answers (1)

Mark Byers
Mark Byers

Reputation: 838974

It depends on the version on Python you are using.

In Python 2.x if you write 'abc' it has type str but this means a byte string. If you want a Unicode string you must write u'abc'.

In Python 3.x if you write 'abc' it still has type str but now this means that is a string of Unicode characters. If you want a byte string you must write b'abc'. It is not allowed to write u'abc'.

        |  2.x                     |  3.x
--------+--------------------------+-----------------------
Bytes   |  'abc' <type 'str'>      |  b'abc' <type 'bytes'>
Unicode | u'abc' <type 'unicode'>  |   'abc' <type 'str'>

Upvotes: 22

Related Questions