MyInternships.in

Python Basics

Python Type Casting

Type casting means converting a value from one data type to another, and Python gives you simple built-in functions to do it on purpose.


What is Type Casting?

Sometimes the data type you have isn't the one you need — for example, input() always returns a string, even if the user typed a number. Type casting lets you convert that value into the correct type before using it.

Explicit Type Casting

Explicit casting means you deliberately convert a value using a built-in function like int(), float(), str() or list().

Converting between types
Python
age_text = "25"
age_number = int(age_text)
print(age_number + 5)   # 30

price = 99
price_text = str(price)
print("Price: " + price_text)

num = "3.14"
num_float = float(num)
print(num_float * 2)

Converting to a List

The list() function converts other iterable data types, such as a string or tuple, into a list.

Python
word = "abc"
letters = list(word)
print(letters)  # ['a', 'b', 'c']

Implicit Type Casting

Implicit casting happens automatically, without you writing any conversion code — Python does it behind the scenes when it is safe to do so, such as combining an int and a float in one expression.

Python converts automatically here
Python
whole = 5
decimal = 2.5
result = whole + decimal
print(result)        # 7.5
print(type(result))  # <class 'float'>
⚠️

Python will NOT automatically convert a string number for math. "5" + 2 raises a TypeError — you must cast it explicitly with int("5") + 2.

Knowing when Python converts automatically versus when you must convert manually will save you from many common beginner errors.

Related Python Topics

Keep learning with these closely related lessons.

Ready to use your Python skills?

Find Python, data science and software internships and fresher jobs across India.

Browse Python Internships