Type Casting | Dataplexa

Type Casting in Python

Type casting (also called type conversion) is the process of converting one data type into another. This is important when you want numbers converted to strings, strings converted to numbers, or when performing calculations and input handling.

Python provides two types of conversions:
1. Implicit Type Casting – done automatically by Python
2. Explicit Type Casting – done manually by the programmer


Implicit Type Casting

Implicit type casting happens automatically when Python converts one data type to another without losing information. This occurs mostly in mathematical operations where smaller data types are promoted to larger ones (e.g., int → float).

x = 5        # int
y = 2.5      # float

result = x + y
print(result)      # 7.5
print(type(result)) # float

Python converted the integer x into a float automatically because a float has higher precision.


Explicit Type Casting

Explicit type casting is when you convert one type to another manually using Python functions. These functions include:

  • int() – converts to integer
  • float() – converts to float
  • str() – converts to string
  • bool() – converts to True/False
  • list() – converts to list
  • tuple() – converts to tuple
  • set() – converts to set

Converting to Integer

int() converts values into whole numbers. Valid inputs include integers, floats, and numeric strings.

a = int(4.9)       # 4
b = int("25")      # 25
c = int(True)      # 1

Note: Strings must contain valid digits; otherwise Python throws an error.


Converting to Float

float() converts values to decimal numbers.

x = float(10)      # 10.0
y = float("3.14")  # 3.14
z = float(False)   # 0.0

Converting to String

str() converts any value into a string. This is extremely useful for printing messages or combining different data types.

name = str(123)
print(name)        # "123"
print(type(name))  # str

Converting to Boolean

Booleans have two values: True and False. Python converts values using logical rules:

  • Zero, empty string, empty list, empty set → False
  • Everything else → True
print(bool(0))        # False
print(bool(10))       # True
print(bool(""))       # False
print(bool("Python")) # True

Converting Between Collections

Python allows converting between lists, tuples, and sets easily.

data = "abc"

print(list(data))   # ['a', 'b', 'c']
print(tuple(data))  # ('a', 'b', 'c')
print(set(data))    # {'a', 'b', 'c'}

These conversions are useful for removing duplicates, reorganizing data, or preparing structures for functions.


Real-World Example: User Input Conversion

User input from forms or command line always comes as a string. To perform calculations, it must be converted into numbers first.

price = input("Enter price: ")  # "100"
price = int(price)

tax = price * 0.18
print("Total:", price + tax)

Real-World Example: Data Cleaning

When processing datasets, numeric fields often appear as strings. Type casting cleans and prepares them for calculations or machine learning tasks.

raw_values = ["10", "20", "30"]

clean_values = [int(x) for x in raw_values]
print(clean_values)   # [10, 20, 30]


📝 Practice Exercises


  1. Convert the string "45" into an integer and add 10.
  2. Convert 9.8 into an integer and observe the behavior.
  3. Convert a boolean True into an integer.
  4. Convert the string "123.45" into a float.
  5. Convert "python" into a list of characters.

✅ Practice Answers


Answer 1:

x = int("45")
print(x + 10)

Answer 2:

print(int(9.8))   # 9

Answer 3:

print(int(True))   # 1

Answer 4:

x = float("123.45")
print(x)

Answer 5:

text = "python"
print(list(text))