Code with the error

weight = "12.5"
print(int(weight) * 2)

Error message (Python 3.12, last line)

ValueError: invalid literal for int() with base 10: '12.5'

Corrected code

weight = "12.5"
print(float(weight) * 2)

Output

25.0

Common causes

  • The text has a decimal point: "12.5".
  • The text is empty.
  • There are letters, units or a comma: "5 kg", "1,000".
  • A word is converted instead of digits: "ten".

Practise in the course

Lesson: String Methods

Frequently asked questions

How do I check that text is a whole number?

text.isdigit() is True for a non-empty string of digits. It is False for a minus sign.

Do spaces around the number matter?

No. int(" 7 ") works; spaces or letters inside the number do not.