Coding/Learn/python/type conversion
Python Beginner Course
01.Introduction to Python
02.Your First Python Program
03.Syntax & Indentation
04.Comments & Documentation
05.Variables
06.Core Data Types
07.Numbers & Arithmetic Operators
08.Text & Strings in Depth
09.Boolean Logic
10.Type Conversion & Casting
11.Interactive User Input
12.Assignment & Shorthand Operators
13.Comparison Operators
14.Logical Operators (and, or, not)
15.If Statements & Conditional Execution
16.If-Else Branching (Dual Paths)
17.If-Elif-Else (Multi-Path Decisions)
18.While Loops & Iteration
19.For Loops & Sequence Traversal
20.The range() Function
21.Lists & Mutable Sequences
22.Tuples & Immutable Data
23.Sets & Unique Collections
24.Dictionaries & Key-Value Stores
25.List Indexing & Slicing
26.Essential List Methods
27.Essential String Methods
28.Functions & Modular Code
29.Function Parameters & Arguments
30.Return Values & Data Flow
31.Exception Handling (try / except)
32.Capstone Mini-Projects
Course Certificate0%

Complete all lessons to earn

LearnType Conversion & Casting
10 / 32 0%
Lesson 10 of 32
6 mins read

Type Conversion & Casting

PreviousNext Lesson
In Plain English

Here is a classic coding trap: If you ask Python to add "50" + "50" (strings with quotes), it gives you "5050" instead of 100! To do real math with text numbers, you must convert them using int("50") or float("3.14"). This is called type casting.

The Mental Model

"Imagine receiving a package labeled "50". Until you open the box and treat it as a real number, it is just cardboard with ink on it. Type casting opens the box so you can do math with it."

Deep Dive: How It Works

Implicit Conversion: Python automatically converts integers to floats during division (e.g. 10 / 2 becomes 5.0).

Explicit Casting: You manually convert data using Python built-in functions:

int(x): Converts text or float to an integer by chopping off decimals (e.g. int("42") is 42, int(9.99) is 9).

float(x): Converts text or integer into a decimal float (e.g. float("19.95") is 19.95, float(5) is 5.0).

str(x): Converts numbers or booleans to text strings (e.g. str(100) is "100").

Syntax Blueprint

num = int("42")       # Converts text "42" to integer 42
price = float("19.99")  # Converts to float 19.99
text = str(100)         # Converts 100 to text "100"

Wrap your variable or literal in int(), float(), or str() to convert its type.

PointCore Rules to Remember

Point star
int() for Whole Numbers: int("42") transforms the text "42" into a real number ready for arithmetic.
Point star
str() for Text Conversion: str(100) transforms a number into text so you can print it alongside other words.
Point star
float() for Decimals: float("9.99") transforms decimal text into floating-point numbers.

Common Beginner Traps & How to Fix Them

Mistake:Trying to int() non-numeric text: int("banana")

Why it happens: Python cannot magically turn words into numbers and throws a ValueError.

How to fix: Ensure strings contain valid digits before passing them to int() or float().

Pro Tip: When converting float strings like "9.99" to an integer, convert to float first: int(float("9.99")).

Live Interactive Example

Hit Run Code to see it live
String Math vs Number Math
Python 3.12
1
2
3
4
5
6
7
8
9
Output Console
Click "Run Code" to view the rendered output.
How it works: Converting "50" to int(50) allowed actual mathematical multiplication to occur.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Convert a string to an integer and multiply

  • PointGiven quantity_str = "8", convert it to an integer.
  • PointMultiply it by 5 and print the result.
1
2
3
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates