Python 2 vs 3, differences

5/5 - (1 vote)

The differences between the two versions of Python are minimal.

The print statement has become the print() function.

Python 2

print “hello webpedia”

become in Python3

print(“hello webpedia”)

The raw_input() function changed to input().

raw_input(‘What is your name?’)

become in Python3

input(‘What is your name?’)

What is difference between input() and raw_input() in Python?

the difference between raw_input() and input() is that the return type of raw_input() is always string, while the return type of input() need not be string only.

Integer division was different in Python 2.x

In Python, there are two kinds of division: integer division (//) and float division (/).

Python 3 syntax

In Python 3.x, integer division produces a floating point result!

print(4 / 3)
print(4 // 3)

Gives the output in Python 3

1.3333333333333333 
1

Python 2 syntax

In Python 2.x when you divided one integer by another integer, the result would always be an integer!

For example:

4/3

Gives the output in Python 1

1

In Python 3, all strings are Unicode

In Python 2, two types could be used to represent strings. One of them, str, was a “byte string” type; it represented a sequence of bytes in some particular text encoding and defaulted to ASCII. The other, Unicode, was (as the name implies) a Unicode string type.

In Python 3, there is one and only one string type. Its name is str and it’s Unicode. Not UTF-8, not UTF-16, not UTF-32!

Sequences of bytes are now represented by a type called bytes.

A bytes instance — if it happens to represent ASCII-encoded text — does still support some operations which let you use it as ASCII, but things that accept strings will generally not accept bytes anymore, and comparisons and other operations no longer work between bytes and str instances.

So bytes turns out to be raw, unencoded, that might be UTF-8, might be UTF-16, it might be ASCII. We don’t know what it is, we don’t know what its encoding is.

ASCII means the American Standard Code for Information Interchange

In ASCII each character is represented by a number between 0 and 256 stored in 8 bits of memory. We refer to 8 bits of memory as a byte of memory.

Python ord()

The ord() function returns an integer representing the Unicode character. The ord() function takes a single parameter, ch – a Unicode character.

>>> print(ord('w'))
119
>>> print(ord('W'))
87
>>> print(ord('&'))
38

In conclusion:

In Python 3 the regular string and the Unicode string are the same.

The byte string and the regular string are different!

Read more here about How Python does Unicode

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Comment

WebPedia.net