Python Casting (Type Conversion in Python)

In Python, casting refers to converting one data type to another. Python provides several built-in functions to perform explicit type conversion (casting). This is useful when you want to force an expression or variable into a specific data type.

Introduction to Type Casting

Type Casting in Python is the process of converting one data type (for example, int to float) into another data type.

In Python, we use different data types to store different kinds of values, such as:

  • Integer (int): Stores whole numbers.
  • Float (float): Stores decimal numbers.
  • String (str): Stores text or characters.
  • Boolean (bool): Stores True or False.
  • Complex (complex): Stores complex numbers.

Sometimes, we need to convert a value from one data type to another to perform calculations or other operations. This is called Type Casting or Type Conversion.

Example: Convert Integer to Float

a = 10

b = float(a)

print(a)
print(b)

Output:

10
10.0

Explanation:

  • Variable a contains an integer value (10).
  • The float() function converts the integer into a floating-point number.
  • Variable b contains the value 10.0.

Types of Type Casting in Python

Python supports two main types of type conversion:

  1. Implicit Type Casting: Done automatically by Python.
  2. Explicit Type Casting: Performed manually using built-in functions.

1. Implicit Type Casting

Definition

Implicit Type Casting is the process in which Python automatically converts one data type into another data type without requiring the programmer to write any conversion function. Python automatically performs this conversion when it is safe and necessary.

Example 1: Integer to Float

a = 10
b = 5.5

c = a + b

print(c)
print(type(c))

Output:

15.5
<class 'float'>

Explanation:

  • a is an integer.
  • b is a float.
  • Python automatically converts integer 10 into float 10.0.
  • The result is 15.5, which is a float.

Integer and Float Multiplication

x = 20
y = 2.5

z = x * y

print(z)
print(type(z))

Output:

50.0
<class 'float'>

Here, Python automatically converts the integer into a float during multiplication.

Advantages of Implicit Type Casting

  1. It is performed automatically by Python.
  2. It reduces the programmer’s effort.
  3. It helps perform operations between compatible data types.
  4. It generally avoids unnecessary data loss.

2. Explicit Type Casting

Definition

Explicit Type Casting is performed manually using type conversion functions. Python provides built-in functions for this purpose.

The programmer decides which data type is required and uses the appropriate function.

Example: Convert String to Integer

a = "100"

b = int(a)

print(b)
print(type(b))

Output:

100
<class 'int'>

Explanation:

  • Variable a contains a string value “100”.
  • The int() function converts the string into an integer.
  • Variable b stores the integer value 100.

Common Type Casting Functions in Python

Python provides several built-in functions for converting data types.

Function Purpose Example
int() Converts into integer int(10.5) → 10
float() Converts into float float(10) → 10.0
str() Converts into string str(100) → “100”
bool() Converts into Boolean bool(1) → True
complex() Converts into complex complex(5) → (5+0j)
list() Converts into list list(“ABC”) → [‘A’,’B’,’C’]
tuple() Converts into tuple tuple([1,2]) → (1,2)
set() Converts into set set([1,2,2]) → {1,2}

A. Integer Conversion (int())

Convert a value to an integer.

  • Floats are truncated (decimal part is removed).
  • Strings must represent a valid integer.
  • Booleans are converted to 0 (False) or 1 (True).
print(int(3.99))
print(int("123"))
print(int(True))
print(int(False))

Output:

3
123
1
0

B. Float Conversion (float())

Convert a value to a floating-point number.

  • Integers are converted to floats.
  • Strings must represent a valid float.
print(float(5))
print(float("3.14"))

Output:

5.0
3.14

C. String Conversion (str())

Convert a value to a string.

  • Works for numbers, Booleans, lists, tuples, etc.
print(str(123))
print(str(3.14))
print(str(True))

Output:

123
3.14
True

D. Boolean Conversion (bool())

Convert a value to a Boolean.

  • 0, 0.0, an empty string (“”), None, and empty collections ([], {}, ()) are False.
  • All other values are True.
print(bool(0))
print(bool(5))
print(bool(""))
print(bool("Hello"))
print(bool([]))
print(bool([1,2]))

Output:

False
True
False
True
False
True

E. Complex Number Conversion (complex())

Convert a number or string to a complex number. A complex number has a real and an imaginary part.

print(complex(5))
print(complex(3.14))
print(complex(5,2))

Output:

(5+0j)
(3.14+0j)
(5+2j)

F. List, Tuple, Set Conversion

You can convert between collections using list(), tuple(), and set().

my_list = [1,2,3]

print(tuple(my_list))

my_tuple = (1,2,2,3)

print(set(my_tuple))

print(list("hello"))

Output:

(1, 2, 3)
{1, 2, 3}
['h', 'e', 'l', 'l', 'o']

Conclusion

Type casting in Python allows you to convert values from one data type to another. Python supports implicit type casting, which happens automatically, and explicit type casting, which is performed manually with built-in functions such as int(), float(), str(), bool(), complex(), list(), tuple(), and set().