Bird
Raised Fist0
Pythonprogramming~20 mins

Type conversion (int, float, string) in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Type Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of mixed type conversions
What is the output of this Python code?
Python
a = '123'
b = 4.56
c = int(float(a)) + int(b)
print(c)
ATypeError
B127.56
C127
D124
Attempts:
2 left
💡 Hint
Remember int() converts float to integer by dropping decimals.
Predict Output
intermediate
2:00remaining
String to int conversion with invalid input
What error does this code raise?
Python
x = '12.3'
y = int(x)
print(y)
AValueError
BTypeError
CSyntaxError
DNo error, prints 12
Attempts:
2 left
💡 Hint
int() cannot convert strings with decimal points directly.
Predict Output
advanced
2:00remaining
Output of chained conversions and operations
What is the output of this code?
Python
val = 7.89
result = str(int(val)) + str(round(val))
print(result)
A"7.89"
B"78"
C"77"
D"79"
Attempts:
2 left
💡 Hint
int(val) drops decimals, round(val) rounds normally.
Predict Output
advanced
2:00remaining
Result of converting boolean to int and string
What is the output of this code?
Python
flag = True
num = int(flag)
text = str(flag)
print(num, text)
A0 False
BTrue 1
CTypeError
D1 True
Attempts:
2 left
💡 Hint
Boolean True converts to 1 as int, and 'True' as string.
🧠 Conceptual
expert
2:00remaining
Understanding type conversion behavior
Which option correctly describes the output of this code snippet?
Python
data = ['1', '2.5', '3']
converted = [int(float(x)) for x in data]
print(converted)
A[1, 2, 3]
B[1, 2.5, 3]
C[1, 3, 3]
DValueError
Attempts:
2 left
💡 Hint
float(x) converts string to float, int() drops decimals.

Practice

(1/5)
1. What does the Python function int() do when applied to a string like '123'?
easy
A. Converts the string to the integer 123
B. Converts the string to the float 123.0
C. Leaves the string unchanged
D. Causes a syntax error

Solution

  1. Step 1: Understand int() function behavior

    The int() function converts a string containing digits into an integer number.
  2. Step 2: Apply int() to '123'

    Applying int('123') converts the string to the integer 123.
  3. Final Answer:

    Converts the string to the integer 123 -> Option A
  4. Quick 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
A. int(3,14)
B. int '3.14'
C. int(3.14)
D. int(3.14.0)

Solution

  1. Step 1: Recall correct function call syntax

    Functions in Python require parentheses around arguments, e.g., int(3.14).
  2. Step 2: Check each option

    int(3.14) uses correct syntax. Others have syntax errors or invalid argument formats.
  3. Final Answer:

    int(3.14) -> Option C
  4. Quick 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
A. 46
B. 45
C. Error
D. 45.67

Solution

  1. Step 1: Convert string to float

    float('45.67') converts the string to the float number 45.67.
  2. Step 2: Convert float to int

    int(45.67) converts the float to integer by dropping the decimal part, resulting in 45.
  3. Final Answer:

    45 -> Option B
  4. Quick 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
A. Use float() instead of int()
B. Add quotes around int
C. Remove the int() conversion
D. Change 'abc' to a string of digits like '123'

Solution

  1. Step 1: Identify cause of error

    Trying to convert 'abc' to int causes a ValueError because 'abc' is not numeric.
  2. Step 2: Fix by using numeric string

    Replacing 'abc' with a string of digits like '123' allows int() to convert successfully.
  3. Final Answer:

    Change 'abc' to a string of digits like '123' -> Option D
  4. Quick 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
A. Use [int(float(x)) for x in list]
B. Use [int(x) for x in list]
C. Use [float(int(x)) for x in list]
D. Use [str(int(x)) for x in list]

Solution

  1. Step 1: Understand list contents

    The list has strings representing integers and floats, e.g., '20.5' and '40.0'.
  2. Step 2: Convert strings to float first

    Direct int() on '20.5' causes error, so convert to float first: float('20.5') = 20.5.
  3. Step 3: Convert float to int

    Then convert float to int with int(20.5) = 20, dropping decimals.
  4. Final Answer:

    Use [int(float(x)) for x in list] -> Option A
  5. Quick 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