Skip to main content

Command Palette

Search for a command to run...

Day 9 – Type Casting in Python: Explicit & Implicit Type Conversion

Updated
6 min readView as Markdown
Day 9 – Type Casting in Python: Explicit & Implicit Type Conversion
S

🚀 Passionate DevOps Engineer with expertise in cloud computing, CI/CD, and automation. Skilled in Linux, Docker, Kubernetes, Terraform, Ansible, and Jenkins. I specialize in building scalable, secure, and automated infrastructures, optimizing software delivery pipelines, and integrating DevSecOps practices. Always exploring new ways to enhance deployment workflows and bridge the gap between development and operations.

What is Type Casting?

Type casting, also called type conversion, is the process of converting a value from one data type to another.

For example, a value stored as a string can be converted into an integer:

a = "10"
b = int(a)

print(b)
print(type(b))

Output:

10
<class 'int'>

Python provides several built-in functions that can be used for type conversion, including:

Function Converts a value to
int() Integer
float() Floating-point number
str() String
bool() Boolean
list() List
tuple() Tuple
set() Set
dict() Dictionary, when the input has a valid dictionary-compatible structure
ord() Unicode code point of a single character
hex() Hexadecimal string representation of an integer
oct() Octal string representation of an integer

Types of Type Conversion

Python type conversion can generally be understood in two ways:

  1. Explicit Type Conversion

  2. Implicit Type Conversion


1. Explicit Type Conversion

Explicit type conversion happens when the programmer manually converts a value from one data type to another.

This is also commonly called explicit type casting.

Python's built-in conversion functions such as int(), float(), and str() can be used for this purpose.

Example

string = "15"
number = 7

string_number = int(string)

total = number + string_number

print("The sum of both numbers is:", total)

Output:

The sum of both numbers is: 22

Here:

string = "15"

is a string.

We explicitly convert it to an integer using:

string_number = int(string)

Now the addition can be performed between two integers.

Important

The string must contain a valid integer representation.

For example:

int("15")

works, but:

int("hello")

raises a ValueError.

Similarly:

int("15.5")

also raises a ValueError because "15.5" is not a valid integer literal for int().


Common Explicit Conversions

String to Integer

a = "10"
b = int(a)

print(b)
print(type(b))

Output:

10
<class 'int'>

Integer to Float

a = 10
b = float(a)

print(b)
print(type(b))

Output:

10.0
<class 'float'>

Integer to String

a = 100
b = str(a)

print(b)
print(type(b))

Output:

100
<class 'str'>

2. Implicit Type Conversion

Implicit type conversion happens when Python automatically converts a value to another compatible type during an operation.

For example:

a = 7
b = 3.0

c = a + b

print(c)
print(type(c))

Output:

10.0
<class 'float'>

Here:

  • a is an int.

  • b is a float.

  • Python converts the integer value 7 to a floating-point value for the addition.

  • The result is a float.

Conceptually:

7 + 3.0
↓
7.0 + 3.0
↓
10.0

This happens automatically, so we do not need to call float() ourselves.


Why Does Python Do This?

Python performs certain automatic conversions when doing so is appropriate and does not require losing information.

For example:

a = 7
b = 3.0

print(a + b)

produces:

10.0

The result is a float because the operation involves a floating-point value.

However, Python does not automatically convert unrelated types in every situation.

For example:

a = "1"
b = 2

print(a + b)

This raises a TypeError.

Python does not automatically convert "1" into 1.

We must explicitly convert it:

a = "1"
b = 2

print(int(a) + b)

Output:

3

Explicit vs Implicit Type Conversion

Feature Explicit Conversion Implicit Conversion
Who performs it? Programmer Python
Also called Type casting Automatic type conversion
Requires conversion function? Usually No
Example int("10") 10 + 2.5
Control Programmer controls the conversion Python performs the conversion

Important Examples

Example 1: Strings

a = "1"
b = "2"

print(a + b)

Output:

12

Why?

Both values are strings, so + performs string concatenation.

It does not perform numerical addition.

Example 2: Explicit Conversion

a = "1"
b = "2"

print(int(a) + int(b))

Output:

3

Here we explicitly convert both strings into integers.

Example 3: Implicit Conversion

c = 1.9
d = 8

print(c + d)
print(type(c + d))

Output:

9.9
<class 'float'>

Python automatically handles the integer and float combination, producing a float result.


Key Takeaways

  1. Type casting means converting a value from one data type to another.

  2. Explicit conversion is performed manually by the programmer.

  3. Functions such as int(), float(), and str() are commonly used for explicit conversion.

  4. Implicit conversion is performed automatically by Python in certain compatible operations.

  5. "1" + "2" produces "12" because both values are strings.

  6. int("1") + int("2") produces 3.

  7. 1 + 2.5 produces 3.5 because the integer participates in a floating-point operation.

  8. Python does not automatically convert unrelated types such as str and int during +.

  9. Type conversion is important when working with user input, calculations, files, APIs, and data processing.


Quick Revision

Type Casting
│
├── Explicit Conversion
│   ├── int()
│   ├── float()
│   ├── str()
│   ├── list()
│   ├── tuple()
│   └── set()
│
└── Implicit Conversion
    └── Python automatically performs
        certain compatible conversions

Remember

"1" + "2"          # "12"
int("1") + int("2") # 3
1 + 2.5            # 3.5

Type casting becomes especially useful when handling values coming from sources such as input(), files, APIs, databases, and user-entered data.


📂 Day 9 Resources

All notes and code for this day are available in the GitHub repository:

https://github.com/SriteshSuranjan/100-Days-of-Python/tree/main/09-Day09-Typecasting-in-Python


100 Days of Python

Part 2 of 10

A structured 100-day journey to learn Python from the fundamentals to advanced concepts through consistent practice and hands-on coding. This series covers Python concepts step by step, including syntax, variables, data types, control flow, functions, data structures, object-oriented programming, exception handling, modules, file handling, libraries, and more. Each day includes clear notes, practical examples, and coding exercises to make learning easier and provide a useful reference for revision. The goal is to build a strong Python foundation that can be applied to automation, software development, data analysis, Artificial Intelligence (AI), Machine Learning (ML), and other areas of technology. Follow along, practice consistently, and build your Python skills one day at a time.

Up next

Day 8 – Exercise 1 Solution: Creating a Calculator with Python Operators

Welcome to Day 8 of 100 Days of Python! In Day 7, I was given my first Python exercise: Create a calculator capable of performing addition, subtraction, multiplication, and division on two numbers an