Python String To Int and Int To String Tutorial

Hey there, In this tutorial let us discuss How To Convert Python String To Int and Int To String, which we can learn with the help of examples.

How To Convert Python String To Int?

To convert string to int in python, we use the int() function. This function can take two parameters: The initial string and the optional base to present the data.

We will use the syntax print(int("STR")) to convert or return the str as an int or integer.

How To Convert Python Int To String?

To convert an integer to string in Python, we use the str () function. This function can take any data type and converts it into a string, including integers.

We will use the syntax print(str(INT)) to convert or return the int as str or string.

Students rapidly discover that Python includes a number of data types that are utilized to distinguish a certain sort of data when learning how to write with it.

For instance, text-based data is represented by Python strings, and full numbers can be represented by Python integers.

Programmers frequently need to convert values between various data types so they can manipulate them in various ways.

Converting a Python string to an integer or an integer to a string is a typical procedure. The built-in Python functions int() and str() can be used to carry out these conversions.

In this article, we’ll look at how to utilize Python’s int() function to change a string into an integer. We’ll also go through how to turn an integer into a string using the str() function.

Data Types In Python

The data you work with when programming in Python will be stored in a variety of different ways. Your information will be saved as a string if you’re dealing with text.

However, your data will be organized as a float if you’re working with a decimal value.

This is significant since there are several ways to modify various types of data. Python thus has to store many types of data using several data types.

In Python, a variety of data types, such as numbers, Booleans, and lists, are used to store data.

One example of data type is a string.

Strings are collections of characters that are used to express information in text.

String are enclosed in single or double quotation marks, like the example below:

String_data = "Hello World!"

#Hello World! is a string that enclosed with double quotation marks.

Both whole numbers and decimal numbers can be stored using the number data type. Whole numbers will be regarded as an integer, whereas decimal values will automatically be converted into float.

Here is an example of a Python integer and floating-point number:

Integer_data = 33

Float_data = 3.33

#33 is an integer data type, because 33 is a whole number.
#3.33 is an float data type, because 33.3 has decimal value.

Data Type Conversion In Python

Data Type conversion in Python is the process of changing one kind of data to another. If you want to convert a string to an integer or an integer to a string, you will be doing a type conversion operation.

Python String To Int

The int() method can used to convert the string to an integer value in python.

int() takes in two parameters: To convert string into an integer and the base you want your data to be represented in, while the second parameter is optional.

The method returns you back the value you gave to int() as an integer.

Here’s the Python int() method syntax.

int(number, base = base)

Here’s an example of int() method being used to convert the String To Int in Python.

print(int("11"))

When you execute the program, this will be the output:

11

Example Program Of Python String To Int In Action

Let’s create a more detailed example program to show the int() method that can be used. First, we want to create a simple registration form.

Let’s start to create a program to perform this function. We will start by using input() method to get the user’s age, month of birth, day of birth and year of birth.

age = input("Enter Your Age:")
month_of_birth = input("Enter Month Of Birth:")
day_of_birth = input("Enter Day Of Birth:")
year_of_birth = input("Enter Year Of Birth:")

print("Your Details:\n")
print("Age:",age)
print("Month Of Birth:",month_of_birth)
print("Day Of Birth:",day_of_birth)
print("Year Of Birth:",year_of_birth)

After the user input their details, this will be the output:

Enter Your Age:26
Enter Month Of Birth:02
Enter Day Of Birth:11
Enter Year Of Birth:1996
Your Details:

Age: 26
Month Of Birth: 02
Day Of Birth: 11
Year Of Birth: 1996

The values of the user inserted are: for age is 26, the month of birth is 02, the day of birth is 11, and the year of birth is 1996. This may look like a number.

However, when we use the type() method we will see what data type is used. We can use the following code to check the data type of the user’s information.

print("Datatypes Used:")
print("Age:", type(age))
print("Month:", type(month_of_birth))
print("Day:", type(day_of_birth))
print("Year:", type(year_of_birth))

This will be the output:

Datatypes Used:
Age: <class ‘str’>
Month: <class ‘str’>
Day: <class ‘str’>
Year: <class ‘str’>

As you can see, our data is stored as a string or str. So, it’s clear that we need to turn our data into an integer. To do this, we can use the following code:

age = input("Enter Your Age:")
user_age = int(age)
month_of_birth = input("Enter Month Of Birth:")
user_month_of_birth = int(month_of_birth)
day_of_birth = input("Enter Day Of Birth:")
user_day_of_birth = int(day_of_birth)
year_of_birth = input("Enter Year Of Birth:")
user_year_of_birth = int(year_of_birth)

print("Your Details:\n")
print("Age:",user_age)
print("Month Of Birth:",user_month_of_birth)
print("Day Of Birth:",user_day_of_birth)
print("Year Of Birth:",user_year_of_birth)

When you execute the program, this will be the output:

Enter Your Age:25
Enter Month Of Birth:02
Enter Day Of Birth:11
Enter Year Of Birth:1996
Your Details:

Age: 25
Month Of Birth: 2
Day Of Birth: 11
Year Of Birth: 1996

As you can see in the output it’s still the same on the first program. So, now we want to use the type() method to see what data types are used now.

Here’s the program to check the data types used:

print("Datatypes Used:")
print("Age:", type(user_age))
print("Month:", type(user_month_of_birth))
print("Day:", type(user_day_of_birth))
print("Year:", type(user_year_of_birth))

This will be the output:

Datatypes Used:
Age: <class ‘int’>
Month: <class ‘int’>
Day: <class ‘int’>
Year: <class ‘int’>

Now, the user stored values is now an integer like we initially wanted.

Python Int To String

The str() method allows you to convert an integer to string in python. The syntax for this method is:

str(number)

Let’s create an example program on how to convert the integer into string value. Here’s the example program:

age = input("Enter Your Age:")
user_age = int(age)
month_of_birth = input("Enter Month Of Birth:")
user_month_of_birth = int(month_of_birth)
day_of_birth = input("Enter Day Of Birth:")
user_day_of_birth = int(day_of_birth)
year_of_birth = input("Enter Year Of Birth:")
user_year_of_birth = int(year_of_birth)

print("Your Details:\n")
print("Age:"+ user_age)
print("Month Of Birth:"+ user_month_of_birth)
print("Day Of Birth:"+ user_day_of_birth)
print("Year Of Birth:"+ user_year_of_birth)

And when the user executes the program, this will be the output:

Enter Your Age:26
Enter Month Of Birth:02
Enter Day Of Birth:11
Enter Year Of Birth:1996
Your Details:

Traceback (most recent call last):
File “
C:\Users\Lenovo\Desktop\conversion\main.py“, line 11, in
print(“Age:”+ user_age)
TypeError: can only concatenate str (not “int”) to str

As you can see, there’s an error occurs. This is because you cannot concatenate a string to an integer. To make our code work correctly, We are going to convert our user details to a string.

Here’s the example below using str() method:

day_of_birth = input("Enter Day Of Birth:")
user_day_of_birth = int(day_of_birth)
string_day_of_birth = str(user_day_of_birth)
year_of_birth = input("Enter Year Of Birth:")
user_year_of_birth = int(year_of_birth)
string_year_of_birth = str(user_year_of_birth)

print("Your Details:\n")
print("Age:"+ string_age)
print("Month Of Birth:"+ string_month_of_birth)
print("Day Of Birth:"+ string_day_of_birth)
print("Year Of Birth:"+ string_year_of_birth)

This will be the output:

Enter Your Age:26
Enter Month Of Birth:02
Enter Day Of Birth:11
Enter Year Of Birth:1996
Your Details:

Age:26
Month Of Birth:2
Day Of Birth:11
Year Of Birth:1996

We have successfully converted our integer to string using str() method.

Conclusion

We have completely discussed the tutorial on how to convert string to int using int() method and int to string using str() method. I hope this tutorial helps you a lot.

Thank You and God Bless!

Leave a Comment