OneBite.Dev - Coding blog in a bite size

convert variable from string to int in python

Code snippet on how to convert variable from string to int in python

  a = "5"
  b = int(a)

This code converts a variable from string to integer. In this example, the variable “a” is a string holding the value “5”. The second line of code casts the string “a” to an integer and assigns the integer value to the new variable “b”. The int() method ignores any whitespace surrounding the value within the quotes, so if “a” was assigned the value “5 ” the int() method would still return the integer 5. Once the integer 5 is assigned to “b”, the value can now be used with other integer operations.

python