Main Tutorials

Python – How to convert String to int

In Python, we can use int() to convert a String to int.


# String
num1 = "88"

# <class 'str'>
print(type(num1))

# int
num2 = int(num1)

# <class 'int'>
print(type(num2))

1. Example

A Python example to add two numbers.

1.1 Add two String directly.


num1 = "1"
num2 = "2"

num3 = num1 + num2

print(num3)

Output


12

1.2 Try it again with int()


num1 = "1"
num2 = "2"

# convert string to int
num3 = int(num1) + int(num2)

print(num3)

Output


3

References

  1. Python docs – int()
  2. Python – How to check the variable’s type?

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments