What is Type Casting?
Type casting in Python means converting the data type of a value into another data type manually. For example, converting a string into an integer, or a float into an integer. This is useful when we want to perform certain operations or calculations that require specific data types.
Type casting is also called type conversion. Python automatically converts data types in some cases (called implicit type casting), but we can also do it manually (called explicit type casting).
- Explicit Type Casting
- Implicit Type Casting
1. Explicit Type Casting
Explicit type casting means we manually convert the data type of a value from one type to another using built-in Python functions. It is the process where the programmer clearly defines which type the value should be converted to.
This is useful when we are taking input from users (which is usually in string format), and we need to perform calculations or comparisons using that input.
Functions used for Explicit Type Casting:
-
Converts x to an integer
This int(x) converts a value to an integer. If it's a float or numeric string, it removes the decimal part.
π§βπ» Example:
print(int(5.99)) print(int("10"))
Output:
5
10
π Explanation: First value 5.99 is truncated to 5. Second value is a string "10", which is converted to integer 10.
-
Converts x to a floating-point number
This float(x) converts a value into a floating-point number.
π§βπ» Example:
print(float(5)) print(float("10.5"))
Output:
5.0
10.5
π Explanation: Integer 5 becomes 5.0. String "10.5" is converted to float 10.5.
-
Converts x to a string
This str(x) converts any value into a string.
π§βπ» Example:
print(str(123)) print(str(True))
Output:
"123"
"True"
π Explanation: The number 123 becomes a string. The boolean value True also becomes a string.
-
Converts x to a boolean (True or False)
bool(x) converts a value to True or False.
- False: 0, 0.0, '', None, [], {}, etc.
- True: All other values
π§βπ» Example:
print(bool(0)) print(bool("hello"))
Output:
False
True
π Explanation: Zero is considered False. Non-empty string "hello" is True.
β Summary Table for Explicit Type Casting
Function | Purpose | Example | Output |
---|---|---|---|
int(x) |
Convert to integer | int("10") |
10 |
float(x) |
Convert to float | float(5) |
5.0 |
str(x) |
Convert to string | str(123) |
"123" |
bool(x) |
Convert to boolean | bool("") |
False |
2. Implicit Type Casting
Implicit type casting means Python automatically converts the data type of a value without any user involvement. Python does this when it feels the conversion is safe and does not lose information.
This usually happens when you are performing operations between different types. For example, if you add an integer and a float, Python automatically converts the integer to float so that no data is lost.
π§βπ» Example:
x = 5 # integer
y = 2.5 # float
z = x + y # Python converts x to float automatically
print(z) # Output: 7.5
Why is Type Casting Important?
- It helps in preventing type-related errors.
- It allows us to work with different data types together.
- We often need it when we take input from users (input is always string).
Common Mistakes in Type Casting:
- Trying to convert non-numeric strings to integers or floats will give an error.
- For example,
int("abc")
will raise aValueError
.
π§βπ» Example:
# β
Correct
a = int("10")
# β Wrong (will give error)
b = int("ten") # ValueError
Summary:
- Type casting means converting one data type to another.
- Explicit casting is done using functions like int(), float(), str(), etc.
- Implicit casting is done automatically by Python during operations.
- Always be careful while converting strings to numbers.
π§βπ» Example:
# Taking string input from user
age_str = input("Enter your age: ")
# Converting string to int
age = int(age_str)
# Displaying result
print(f"You will be {age + 5} years old after 5 years.")