How to Convert Python String to Int:
To convert a string to integer in Python, use the int()
function. This function takes two parameters: the initial string and the optional base to represent the data. Use the syntax print(int("STR"))
to return the str
as an int
, or integer.
How to Convert Python Int to String:
To convert an integer to string in Python, use the str()
function. This function takes any data type and converts it into a string, including integers. Use the syntax print(str(INT))
to return the int
as a str
, or string.
When learning how to code with Python, students quickly find that Python includes a number of data types that are used to distinguish a particular type of data. For example, Python strings are used to represent text-based data, and integers can represent whole numbers. When you’re programming, you may want to convert values between different data types so you can work with them in different ways.
One common operation is to convert a Python string to an integer or an integer to a string. Python includes built-in methods that can be used to perform these conversions: int()
and str()
.
In this tutorial, we’ll explore how the int()
method can be used to convert a string to an integer in Python. We’ll also discuss how to use str()
to convert an integer to a string.
Python Data Types
When you’re programming in Python, the data you are working with will be stored in a number of different ways. If you’re working with text, your data will be stored as a string. But if you’re working with a decimal number, your data will be structured as a float.
This is important because each type of data can be manipulated in different ways. Thus, Python needs to store different sorts of data using different data types. There are a number of data types in Python that are used to store data, including numbers, Booleans, and lists.
In this article, we will focus on two of these data types: strings and numbers.
In Python, strings are enclosed within single or double quotes and are used to represent text-based information. Here’s an example of a string:
text_data = "This string contains text-based data."
The number data type is used to store both decimal and whole numbers. Decimal numbers will automatically be converted into a float, and whole numbers will be considered an integer. Here’s an example of an integer and a floating-point number in Python:
example_integer = 22 example_float = 2.22
Converting a data type to another form of data is called type conversion. If you want to convert a string to an integer or an integer to a string, you will be performing a type conversion operation
Python String to Int
The int()
method can be used to convert a string to an integer value in Python.
int()
takes in two parameters: the string to convert into an integer and the base you want your data to be represented in. The second parameter is optional.
The method returns the value you passed through int()
, represented as an integer. Here’s the syntax for the Python int()
method:
int(number, base=base)
Here’s an example of the int()
method being used to convert a string to an integer:
print(int("12"))
Our code returns:
12
An Example of String to Int in Action
Let’s use a more detailed example to show how the int()
method can be used. Say that we are creating a sign up form for a children’s game that asks for the user’s age. We need this value to be stored as an integer in our database. But when a user inserts the value in our program their age becomes a string.
Let’s create a program that performs this function. We’ll start by using the input()
method to get a user’s age:
raw_user_age = input("What is your age?") print(raw_user_age)
When our user enters a number, it will be printed out to the console. Here’s what happens when we run our program:
What is your age? 12 12
The value the user inserted is 12
. This may look like a number. However, when we use the type()
method to check the data type for our user’s age, we see it isn’t a number. We can use the following code to check the data type of our user’s age:
print(type(raw_user_age))
Our code returns:
<class 'str'>
As you can see, our data is stored as a str
, or a string. So, it’s clear that we need to convert our data to an integer. We can do so by using the following code:
raw_user_age = input("What is your age?") user_age = int(raw_user_age) print(user_age)
Here’s what happens when we execute our code and insert the value 12
:
What is your age? 12 12
Our code returns the same output as above, but 12
is now stored as a integer. We can use the type()
method to check:
print(type(user_age))
Our code returns:
<class 'int'>
Now our value is stored as an integer like we initially wanted.
Python Int to String
The str() method allows you to convert an integer to a string in Python. The syntax for this method is:
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
str(number_to_convert)
Let’s walk through an example to show how this method works. In our earlier example, we converted the user’s age to an integer using this code:
raw_user_age = input("What is your age?") user_age = int(raw_user_age)
Let’s say that we want to print a message with our user’s age to the console. We could do this by using the following code:
print("Your age is: " + user_age)
Our code returns an error:
Traceback (most recent call last): File "main.py", line 3, in <module> print("Your age is: " + user_age) TypeError: can only concatenate str (not "int") to str
This is because you cannot concatenate a string to an integer like we have tried to do above. To make our code work, we’re going to have to convert our user’s age to a string. We can do this by using the str()
method:
raw_user_age = input("What is your age?") user_age = int(raw_user_age) as_string = str(user_age) print("Your age is: " + as_string)
Our code returns:
What is your age?
12
Your age is: 12
We’ve successfully converted our integer to a string using str(). Both values are now strings, which means that we can now concatenate the message Your age is:
with the user’s age.
Python Convert List of Strings to List of Integers
Suppose we have a list of strings that contains the grades students have earned in a computer science class. We want to convert every grade in our list into an integer. To do this, we can use a Python list comprehension.
A list comprehension makes it easy to create a new list based on the contents of an existing list. In this case, we will use a list comprehension to turn a list of strings into a new list of integers.
Consider this code:
grades = ["82", "84", "71", "64", "66"] new_grades = [int(g) for g in grades] print(new_grades)
First, we define a list called “grades”. This list contains strings. We then write a list comprehension that converts every value in our “grades” list into an integer. Our list comprehension does this by iterating over every value in “grades” and changing their data type using the int() function.
At the end of our program, we print our new list of integers:
[82, 84, 71, 64, 66]
Our code has successfully converted our list of strings into a list of integers.
Conclusion
The int() method is used to convert a string to an integer in Python. This can be useful if you need to store a value as an integer or perform mathematical operations on a value stored as a string. The str() method is used to convert an integer to a string.
Now you’re ready to convert strings to integers in Python like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
How can I convert a variable with more than one component of strings to int?
for example I have a vector ( [“3″,”5″,”7”]). How can I convert in to [3,5,7]?
Hello there! I have added a “Python Convert List of Strings to List of Integers” section to the tutorial which answers your question. Thank you for your comment.