Scripting

How to Change String to Integer in Python

How_to_Change_String_to_Integer_in_Python

A string is a data type used to represent text enclosed in single or double quotes while an integer is a whole number that can be positive, negative, or zero and not enclosed in single or double quotes. Sometimes we need to convert one data type to the other data type for performing certain operations. To convert a string to an integer the string must consist of integers only which will be discussed in this article

By using the int() function

By using the int () built-in function, we can convert a string to an integer where this function takes one parameter: a string, and returns the integer.

S = "45"

print(type(S))

N=int(S)

print(type(N))

Text Description automatically generated

Figure 1: Using int() function

Output:

Figure 2: Output

In the above example, we have a string ‘S’ that belongs to the string class and by using the int() function we have converted the string ‘S’ to an integer. Now if check its data type by using the type function we’ll see it belongs to the integer class.

The need for conversion from string to integer

User input in Python is read by the input() function whereas the input() returns string. In other words, user input is stored in a string. Most of the time, the input values need to be converted to numeric values to perform arithmetic operations. To illustrate the need for conversion of string input to numeric values, see the following code segment:

print("Reading user input")

total_students=input("Enter total number of students")

presnt_students=input("Enter number of present students")

absent_students=total_students-presnt_students

print("Number of absent students: "+ absent_students)

Text Description automatically generated

Figure 3:Using input() function

Output:

Figure 4: Output

When the above code is executed, a type error will be generated as the input() function takes the input from the user as a string. Arithmetic operators operate on numeric values only. To perform arithmetic operations on user input, type conversion from string to integer is needed. This conversion is done using the int() function. The following code segment demonstrates how to apply an arithmetic operation on user input.

print("Reading user input")

total_students=input("Enter total number of students")

present_students=input("Enter number of present students")

t=int(total_students)

p=int(present_students)

absent_students = t - p

print("Number of absent students: "+ str(absent_students))

Text Description automatically generated

Figure 5: Converting string to integer

Output:

Text Description automatically generated

Figure 6: Output

The above code takes the total number of students and the number of present students from the user. It then calculates the number of absent students by subtracting the number of present students from the total number of students. To perform this calculation, the input values need to be converted to an integer by using int(). The student’s Variable is assigned the result of subtraction of converted values (t and p).

It is important to note the use of the str(students) function in the last line of the above code. The + operator is used to add two numeric values but the same operator is used to concatenate two strings. Since students are a numeric (int) variable, it needs to be converted to a string to concatenate a message (string constant) to print a formatted output of the operation.

How to Change Float to Integer in Python

The float and int both types are numeric data types in Python. The difference is that the float type represents the fractional values while the int holds only the whole values. For instance, a value of 4.45 cannot be stored in int. An int variable will store its integer part, i.e. 4, while a float variable will store 4.45. The int() function returns the int value of the parameter float value passed to it, i.e. 4 it returns the integer part of the parameter. Consider the following code fragment

print("Converting float to int")

x= 215

y=6

z=x/y

print(str(x)+"/"+str(y)+"="+str(z))

print("Integer part of z = " + str(int(z)))

Figure 7: Using int() function

Output:

Text Description automatically generated

Figure 8: Output

The code fragment shown above demonstrates how a variable z is assigned a value obtained by dividing the value of variable x by the value of another variable y. The results of division are not a whole number but contain a fractional part too. The value of the z variable is displayed to the user by using the print() function whereas the last line of code uses the int() function to get only the integer part of the variable z.

Similar Posts