Convert String to Integer in Python

  • Post category:String

In Python, you can use a int() function to convert a string to integer. In the below code, we will convert a string to an integer. The int() function can take two arguments. The first argument is the string to be converted. The second argument is the base of the string. The default base is 10.

Step 1: Create a string

str = "123"
print("str = ", str)
print("str type = ", type(str))

Step 2: Convert string to integer

print("str = ", int(str))
print("str type = ", type(int(str)))


str =  123
str type =  <class 'str'>
str =  123
str type =  <class 'int'>

Free resources to learn advanced skills: AiHints and CodeAllow