What are the key differences between Python 2.7.x and Python 3.x?

Python 3.0 was released in December 2008. It was designed to rectify certain flaws in earlier versions. Python 3.0 doesn't provide backward compatibility. That means a Python program written using version 2.x syntax doesn't execute under the Python 3.x interpreter. Version 2.7 is the final major release in the Python 2.x series.

The guiding principle of Python 3 was: "reduce feature duplication by removing old ways of doing things".

Although there are quite a few differences in usage of these two versions, the most obvious ones are mentioned below −

Print Statement Vs Function

print is a keyword in Python 2.7 but has been included as a built-in function in Python 3.x. As a result, parentheses are mandatory when using them in Python 3 code −

# Python 2 style - would cause SyntaxError in Python 3
# print "Hello World"

# Python 3 style - works in both versions
print("Hello World")

The output of the above code is −

Hello World

Input Functions

Python 2 has a separate function for string and a different one to evaluate input, while Python 3 has a single function that returns strings. The raw_input() function from Python 2.7 has been deprecated. The input() function treats the received data as a string only.

# Python 2
user_input = raw_input("Enter text: ")  # Returns string
evaluated = input("Enter expression: ")  # Evaluates as Python code!

# Python 3
user_input = input("Enter text: ")      # Always returns string

Integer Division

The functionality has been changed in Python 3. In Python 2.x, 5/2 results in 2, but in Python 3.x, 5/2 is 2.5 −

# Division behavior in Python 3
print(5 / 2)    # True division - returns float
print(5 // 2)   # Floor division - returns integer
print(type(5 / 2))
print(type(5 // 2))

The output shows the difference −

2.5
2
<class 'float'>
<class 'int'>

String Handling

In Python 3.x, a string is Unicode by default. In Python 2.x, the string has to be explicitly defined as Unicode by prefixing it with 'u' (e.g., u'hello') −

# Python 3 - All strings are Unicode by default
text = "Hello ??"
print(text)
print(type(text))

# For binary data, use bytes
binary = b"Hello"
print(binary)
print(type(binary))

The output demonstrates string types −

Hello ??
<class 'str'>
b'Hello'
<class 'bytes'>

Long Integers

In Python 3.x, integer objects are long by default. In Python 2.x, an integer has to be postfixed by L (e.g. 100L) −

# Python 3 - All integers are "long" by default
x = 1000000000000000000000
print(x)
print(type(x))

# No need for 'L' suffix
y = 999999999999999999999999999999
print(y)

The output shows unified integer type −

1000000000000000000000
<class 'int'>
999999999999999999999999999999

Exception Handling

The syntax for catching exceptions was improved in Python 3 to use the as keyword instead of a comma −

# Python 3 exception handling syntax
try:
    result = 10 / 0
except ZeroDivisionError as e:
    print(f"Error caught: {e}")
    print(f"Error type: {type(e)}")

The output shows proper exception handling −

Error caught: division by zero
Error type: <class 'ZeroDivisionError'>

Summary

Feature Python 2.x Python 3.x
Print Statement: print "hello" Function: print("hello")
Input raw_input() and input() Only input() (returns string)
Division 5/2 = 2 (integer) 5/2 = 2.5 (float)
Strings ASCII by default, u"" for Unicode Unicode by default
Integers long needs L suffix All integers are long
Exceptions except Error, e: except Error as e:

Conclusion

Python 3 simplified many inconsistencies from Python 2, making the language more intuitive and consistent. While migrating requires syntax updates, Python 3's improvements in string handling, division behavior, and exception syntax make it the preferred choice for new projects.

Updated on: 2026-03-24T17:19:58+05:30

631 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements