A Simple Difference of String in Python 2.x and Python 3.x – Python Tutorial

By | September 16, 2019

There are some dfferences between python 2.x and python 3.x. In this tutorial, we will discuss some differences on python string. You should notice these differences when your are using python string.

python string

In python 3.x

Python string contains unicode string and byte string.

str: unicode string

s = '博客'

The result is:

<class 'str'>

bytes: a byte string

s = b'https://www.tutorialexample.com'

The result is:

<class 'bytes'>

You should notice:

SyntaxError: bytes can only contain ASCII literal characters.

so this is error:

s = b'博客'

In python 2.x

str: is a byte string

bytes: the same to str

unicode: a unicode string, it can be create as u’博客’

In summary, in python 3.x, string is a unicode string, however, string should be created with u as a unicode string in python 2.x.