Core programming fundamentals · Lesson 8 of 60 · Code lesson, JavaScript or Python · Optional
Type conversion
Type conversion changes a value from one type to another, such as the text "5" into the number 5.
Implicit conversion happens automatically, while explicit conversion is when you ask for it with a function.
Input arrives as text, so numbers you read must be converted before you do math with them.
Worked example
In JavaScript, Number("5") turns text into a number.
const text = "5";
const number = Number(text);
console.log(number + 1);Output: 6
In Python, int("5") turns text into a whole number.
text = "5"
number = int(text)
print(number + 1)Output: 6
Your turn
The program reads two numbers, but they arrive as text, so + joins them instead of adding. Convert them so it prints their sum.
Your program is checked with these 2 inputs:
Input:
3 4
Expected output:
7
Input:
10 20
Expected output:
30
Loading your practice…
Preparing your workspace…