Python - Convert String to Int

  • Post author:
  • Post category:Python
  • Reading time:1 mins read

In Python, use int() function to convert a string to int (integer).

INT() Function Example

a_string = "23"
n_int = int(a_string)
n_sum = n_int + 10
print(n_sum)

Output

33

But if the value of a string is in double, then you will get the error. For example:

a_string = "23.98"
n_int = int(a_string)
n_sum = n_int + 10
print(n_sum)

Output

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

See also:

Vinish Kapoor

An Oracle Apex Consultant, Oracle ACE, and founder of foxinfotech.in and orclqa.com a question and answer forum for developers.