python

How to Convert a String to an integer in Python

In this tutorial, we are going to see how to convert String to an integer in Python. Python provides different types of variables for programmers. We can use data types like int, float, string, list, set… in our applications. When using different types of variables, it may be necessary to convert these to different types.

Just like the str() function, Python also offers a handy built-in function that takes an object of type String as an argument and returns the corresponding Integer object.
 

 

How to Convert a String to an integer in Python
# Here nbr is an object of type string
nbr = "25"
print(nbr )
# Converting string to integer
int_nbr = int(nbr )
print(int_nbr)

Output:

25
25

Here, although the output is the same but you have to keep in mind that the first line displays an object of type string, while the second line displays an integer object.
 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *