int(), float(), and str() are built-in conversion functions. Each creates a new value.
age = int("12")Line-by-line explanation
"12"begins as a string.int("12")creates the integer12.age =stores the new integer.
distance = float("2.5")Line-by-line explanation
"2.5"begins as text.float("2.5")creates the decimal number2.5.- The float is stored in
distance.
score_text = str(100)Line-by-line explanation
100begins as an integer.str(100)creates the string"100".- The string is stored in
score_text.
Conversion works only when the value makes sense. Python cannot turn "twelve" into an integer because those letters are not digits.
Tiny Practice: Convert Numeric Text
Convert "30" into an integer stored in minutes.
Starter template
minutes = CHANGE_ME
print(type(minutes))Line-by-line explanation
- The first line needs a conversion.
- The second line checks the result.
Hint
- Use
int. - Put
"30"inside its brackets. - The expected type is
int.
Show Solution
minutes = int("30")
print(type(minutes))Line-by-line explanation
int("30")creates integer30.- The integer is stored in
minutes. - The second line confirms
int.