100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Type Casting in Python

How to convert values between Python data types explicitly with functions like int(), float(), and str(), and how implicit coercion works.

Basics & Data TypesBeginner8 min readJul 7, 2026
Analogies

1. Introduction

Type casting (or type conversion) is the process of converting a value from one data type to another, such as turning the string "25" into the integer 25. Python supports two kinds of type conversion: implicit conversion, which the interpreter performs automatically in certain expressions, and explicit conversion, where you call a built-in function to convert a value yourself.

🏏

Cricket analogy: When a scorer converts overs bowled (a decimal like 4.3) into a total ball count that's implicit conversion; when a captain manually asks the third umpire to review a decision, that's explicit — Python does the same converting '25' to 25 automatically or via int().

Type casting is essential whenever data arrives in the 'wrong' type for what you need to do with it — most commonly when reading user input (always a string) and needing to perform arithmetic on it, or when formatting numeric results as text for display.

🏏

Cricket analogy: When a scorer keys in a batsman's score as text from a manual sheet, it must be cast to an integer before totaling the innings score, just as Python casts an input() string before doing arithmetic on it.

2. Syntax

python
int("100")      # str -> int
float("3.14")   # str -> float
str(42)         # int -> str
bool(0)         # int -> bool
list("abc")     # str -> list

# implicit conversion
result = 5 + 2.5   # int + float -> float

3. Explanation

Implicit type conversion happens automatically when Python combines two compatible numeric types in an expression; for example, adding an int and a float produces a float because Python 'widens' the int to avoid losing precision. No data is lost and no function call is needed.

🏏

Cricket analogy: When a team's batting average (a float like 32.5) is added to a whole-number wicket count, Python automatically widens the int to a float so no precision is lost, like a scorer never rounding off a partial run.

Explicit type conversion requires calling a constructor function such as int(), float(), str(), list(), or bool() directly on a value. These functions attempt to reinterpret the input as the target type and raise an exception if the conversion is not meaningful.

🏏

Cricket analogy: Explicitly calling int() on a scoreboard string like '187' works fine, but calling int() on 'not out' raises an exception, just as a scorer can't force a non-numeric result into a run total.

bool() treats 0, 0.0, empty strings, empty collections, and None as falsy, and virtually everything else — including non-zero numbers and non-empty strings like "False" — as truthy.

Common gotcha: int("3.14") raises ValueError: invalid literal for int() with base 10: '3.14'. You cannot convert a string containing a decimal point directly to int — you must first convert to float and then to int, e.g. int(float("3.14")).

4. Example

python
a = 5
b = 2.5
c = a + b
print(c, type(c))

x = "100"
y = int(x)
print(y, type(y))

z = "3.14"
f = float(z)
print(f, type(f))

n = int(f)
print(n)

s = str(42)
print(s, type(s))

lst = list("abc")
print(lst)

print(int(True), int(False))

5. Output

text
7.5 <class 'float'>
100 <class 'int'>
3.14 <class 'float'>
3
42 <class 'str'>
['a', 'b', 'c']
1 0

6. Key Takeaways

  • Implicit conversion happens automatically for compatible numeric types (int + float -> float).
  • Explicit conversion uses constructor functions: int(), float(), str(), list(), bool(), etc.
  • int("3.14") fails directly — convert via float() first, then int() to truncate.
  • Converting float to int truncates toward zero, it does not round.
  • bool() follows Python's truthy/falsy rules for the source value.
  • str() can convert virtually any object to its readable text representation.

Practice what you learned

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#TypeCastingInPython#Type#Casting#Syntax#Explanation#StudyNotes#SkillVeris