How To Convert A String To Int In Python

Python comes equipped with a variety of data types, each of which may be used to differentiate a certain kind of data. For instance, strings in Python are used to represent data that is based on text, while integers are utilized to represent entire numbers. Converting values from one data type to another allows you to interact with the data in a variety of different ways, which may be useful while you’re doing programming.

The process of converting a Python string into an integer is a frequent activity. The int() and str() built-in methods are included in Python and may be used to make the aforementioned conversions ().

In this we will investigate the Python int() function, which may be used to transform a text into an integer.

Read: How to Create an executable from a Python program

Using the int() method in Python will allow you to convert a string to an integer. This function requires two arguments in order to work: the original string and a base to represent the data (which is optional). If you want to return the string in the form of an integer, you may do so by using the syntax print(int(“STR”)).

CODE:

Syntax: int (string)

num = ’10’

# check and print type num variable

print(type(num))

# convert the num into string

converted num = int(num)

# print type of converted num

print(type(converted num))

# We can check by doing some mathematical operations

print(converted num + 20)

INPUT:

How To Convert String To Int In Python

OUTPUT:

 


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

Marianne elanotta

As a graduate in communication technologies, Marianne likes to share the latest technological advances in various fields. She likes to program in Java OO and Javascript and prefers to work on Open source operating systems. She likes to play chess and computer games in her spare time along with her 2 kids.

Leave a Reply