Python Type Conversion: Implicit and Explicit Casting with Examples
This article explains Python's type conversion mechanisms, distinguishing implicit automatic casting from explicit conversion using functions like int(), float(), and str(), and provides multiple code examples demonstrating how different data types interact, the resulting outputs, and common errors such as TypeError.
Python data type conversion can be divided into two categories: implicit conversion, which Python performs automatically, and explicit conversion, which requires the use of type functions.
Implicit Type Conversion
In implicit conversion, Python automatically changes a value from a lower‑precision type (e.g., int) to a higher‑precision type (e.g., float) to avoid data loss.
Example:
num_int = 123
num_flo = 1.23
num_new = num_int + num_flo
print("datatype of num_int:", type(num_int))
print("datatype of num_flo:", type(num_flo))
print("Value of num_new:", num_new)
print("datatype of num_new:", type(num_new))Output:
datatype of num_int: <class 'int'>
datatype of num_flo: <class 'float'>
Value of num_new: 124.23
datatype of num_new: <class 'float'>This shows that the integer is implicitly converted to a float during the addition.
Explicit Type Conversion
Explicit conversion uses built‑in functions such as int(), float(), and str() to force a value into a desired type.
Examples:
# int() examples
x = int(1) # 1
y = int(2.8) # 2
z = int("3") # 3
# float() examples
x = float(1) # 1.0
y = float(2.8) # 2.8
z = float("3") # 3.0
w = float("4.2") # 4.2
# str() examples
x = str("s1") # 's1'
y = str(2) # '2'
z = str(3.0) # '3.0'When trying to add an integer and a string directly, Python raises a TypeError because implicit conversion is not possible between these types:
num_int = 123
num_str = "456"
print(num_int + num_str) # TypeErrorTo perform the addition, the string must be explicitly converted:
num_int = 123
num_str = "456"
num_str = int(num_str) # convert to int
num_sum = num_int + num_str
print("Result:", num_sum) # 579
print("type of result:", type(num_sum))Output:
Result: 579
type of result: <class 'int'>These examples illustrate how Python handles both automatic (implicit) and manual (explicit) type conversions, the situations where each is applicable, and how to avoid common errors.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
