Type conversion helps you change data from one kind to another, like turning numbers into words or words into numbers. This is useful because computers treat different types of data differently.
Type conversion (int, float, string) in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
int(value) float(value) str(value)
int() converts to whole numbers (no decimals).
float() converts to numbers with decimals.
str() converts any value to text.
Examples
Python
int('123') # converts string '123' to number 123
Python
float('3.14') # converts string '3.14' to number 3.14
Python
str(100) # converts number 100 to string '100'
Python
int(3.99) # converts float 3.99 to int 3 (drops decimals)
Sample Program
This program shows how to convert text input to numbers and numbers to text for printing.
Python
user_input = '42' number = int(user_input) print(f"User input as number: {number}") pi_text = '3.1415' pi_number = float(pi_text) print(f"Pi as number: {pi_number}") score = 99 score_text = str(score) print("Score as text: " + score_text)
Important Notes
Trying to convert text that is not a number (like 'hello') to int or float will cause an error.
Converting float to int removes decimals without rounding.
Use type conversion to avoid errors when mixing types in operations or printing.
Summary
Type conversion changes data between numbers and text.
Use int(), float(), and str() to convert types.
Be careful with invalid conversions to avoid errors.
Practice
1. What does the Python function
int() do when applied to a string like '123'?easy
Solution
Step 1: Understand int() function behavior
Theint()function converts a string containing digits into an integer number.Step 2: Apply int() to '123'
Applyingint('123')converts the string to the integer 123.Final Answer:
Converts the string to the integer 123 -> Option AQuick Check:
int('123') = 123 [OK]
Hint: int() turns digit strings into whole numbers [OK]
Common Mistakes:
- Thinking int() converts to float
- Expecting string unchanged
- Confusing with syntax error
2. Which of the following is the correct syntax to convert the float number 3.14 to an integer in Python?
easy
Solution
Step 1: Recall correct function call syntax
Functions in Python require parentheses around arguments, e.g.,int(3.14).Step 2: Check each option
int(3.14) uses correct syntax. Others have syntax errors or invalid argument formats.Final Answer:
int(3.14) -> Option CQuick Check:
int(3.14) is valid syntax [OK]
Hint: Use parentheses with int() for conversion [OK]
Common Mistakes:
- Omitting parentheses
- Using commas instead of dots
- Adding extra dots in numbers
3. What is the output of this code?
value = '45.67' result = float(value) print(int(result))
medium
Solution
Step 1: Convert string to float
float('45.67')converts the string to the float number 45.67.Step 2: Convert float to int
int(45.67)converts the float to integer by dropping the decimal part, resulting in 45.Final Answer:
45 -> Option BQuick Check:
int(float('45.67')) = 45 [OK]
Hint: int() drops decimals, does not round [OK]
Common Mistakes:
- Expecting rounding to 46
- Printing float instead of int
- Confusing string and number types
4. The code below causes an error. What is the fix?
num = 'abc' converted = int(num) print(converted)
medium
Solution
Step 1: Identify cause of error
Trying to convert 'abc' to int causes a ValueError because 'abc' is not numeric.Step 2: Fix by using numeric string
Replacing 'abc' with a string of digits like '123' allows int() to convert successfully.Final Answer:
Change 'abc' to a string of digits like '123' -> Option DQuick Check:
int('123') works, int('abc') errors [OK]
Hint: int() needs numeric strings, not letters [OK]
Common Mistakes:
- Using float() on non-numeric string
- Removing conversion but expecting int
- Adding quotes around function name
5. You have a list of mixed strings representing numbers:
['10', '20.5', '30', '40.0']. How do you convert all to integers correctly in Python?hard
Solution
Step 1: Understand list contents
The list has strings representing integers and floats, e.g., '20.5' and '40.0'.Step 2: Convert strings to float first
Direct int() on '20.5' causes error, so convert to float first:float('20.5')= 20.5.Step 3: Convert float to int
Then convert float to int withint(20.5)= 20, dropping decimals.Final Answer:
Use [int(float(x)) for x in list] -> Option AQuick Check:
int(float('20.5')) = 20 [OK]
Hint: Convert string to float, then to int for decimals [OK]
Common Mistakes:
- Trying int() directly on float strings
- Swapping int and float conversions
- Converting to string instead of int
