← Go Back

How to Convert a String to a Number

To convert a string to an integer, use the int() built-in function.

>>> int("5")
5

Use the second argument to convert to a different base (defaults to 10). For example:

# Hexadecimal conversion.
>>> int("ff", 16)
255
# Binary.
>>> int("101", 2)
5

To convert to a floating point number, use the float() built-in:

>>> float("3.14")
3.14


strings integers conversion


🐍 You might also find interesting: