Python

How to String Int Conversions in Python

The Python language allows us to convert integers, strings, and float values into each other. The simplest way of conversion is to use (), str(), and float() methods. In Python, strings are defined as characters with double or single quotes. This article will assist you in converting the string data type into integers.

Prerequisites

To understand the concept of conversion, you need any programming tool having Python in running condition that is a Spyder tool. To have the involvement of Linux, you need to install the Ubuntu software on the virtual box in the system with the user privileges.

Example 1:

In this illustration, we will consider an input-taking string. A variable is declared that will store the string value. The string will be added by the user. i.e., the screen will prompt a message of “your age?”. To find the data type of the variable we use the following code:

Print(type(variable name))

Now, to find the output, open the Ubuntu terminal and write the following command:

$ python3 ‘/home/aqsa/Documenrs/resulttemp.py’

After the message of age is displayed, the user will enter the age. Then, the age and type will be shown. This type is in string. Now, we will see the conversion of a string to an integer by using the same example to elaborate quickly.

In this code, the input that the user provides us is converted into an integer first. And, this is done by using the following command:

User_age_new = int(user_age)

This will convert the string value into the integer. After conversion, according to the code, the value will be printed, and the type of the value is also obtained from the code as described earlier.

Execute the code on the Ubuntu terminal:

In the output, you can see that after providing the age. The result shows the type of the value/ age as an integer. That’s how we have converted a string into an integer value.

Example 2:

In this example, we don’t ask the user to give us input. We use two variables to apply the multiplication operators on them by converting their types. There are two numbers as an example below. We have performed both functions in a single line code i.e., conversion of data type and the multiplication:

Result =int(number1) * (number2)

First, both the numbers are multiplied, and the result is converted into the integer value. After printing the result, we will know about the data type of the subsequent value.

The output depicts the data type of the result, which is an integer.

String’s List to Integer’s List Conversion

In this example, we will take a list of string elements as input and will give a list of integers as output. The list of the string contains the marks. Each character in the string is switched into an integer by using the cited code.

new_marks = [int(g) for g in marks]

After, conversion takes the print of the list of integers and the data type.

Getting Integer From the String

This function is different from the one described above because instead of converting the whole string value, the integer part is extracted from the provided input. Here, we have imported a “re” module that helps in extraction. Then, the string is defined. We have firstly displayed the type of the variable to ensure the conversion. Now, the following command is used for the accomplishment of the given function.

Result = (int(re.search(r’\d+’ , string.group()))

In this function, the string is taken as an input. With the help of a “re” module, the “r” object is created that helps in conversion. The integer part that “500” is extracted.

The output is shown in the image shown below. You can see that the integer part is separated, and the type is shown as an integer.

String to Int Conversion With Commas

The string value is taken in a variable and is converted in the same way as discussed in the above examples. But the function of comma removal is defined here:

Sample1 = int(sample.replace(,, ‘ ‘))

This given function works in a way that the commas are replaced by the empty value, which means nothing so that the commas are removed from the resultant value. After removal, the value is then converted into an integer value.

Let’s have a look at the output. We have printed a string “the integer value is” and then the resultant value. The value type is integers. So the string value is switched into an integer value.

Using the Base Elements for Conversion of String to Int

In this type of conversion, the base values are provided. We have used different base values in this example for the conversion. When the bases are used after their function, the value is converted into integers. The simple syntax used for this conversion is as follows:

Class int (z,base=12)

Different variables will show different answers with more than one base value. After writing the code, execute it on the Linux terminal using the same command defined above.

You can view the output appended below:

String to int Conversion for Leap Years

This example is different from the other ones. As in this illustration, we will take a string value from the user, and the program will tell whether the value is true or not. In simple words, the user will enter a year, and this year is first converted into an int. After conversion, the leap year formula is used to calculate if it is a leap year or not.

Now, understand the code. We will write the code in the body to attempt if any exception is present; it can be handled. At first, we will print a message if the user is not providing the year in “2000” format. If this situation is false, then move forward. Calculate the leap year by taking the modulus of the leap year with 4. If the remainder is 0, then it’s a leap year, and if not, then it is not a leap year.

For obtaining the output, write the following command. Next, the user will enter the year, that value is then calculated, and the result is displayed below:

Conclusion

From this article, we come to know how to convert two data types into one another. The integer value is obtained by performing different functions on the string value.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.