×

How to Convert int to str in Python

How to Convert int to str in Python

In the last article, we studied how we convert a variable with string datatype to integer datatype and discussed different data types available in Python. In this section, we will see the ways & methods that can convert an integer data type to a string data type.

Four ways can help us in the conversion-

  1. Using str()
  2. Using f-string
  3. Using the "%s" keyword
  4. Using. format () function

First, let us discuss how we can convert int to str in python using str()

 int= 15                       #declaration of variable
 print(type(int))
 str_var=str(int)              #converting the data type
 print(type(str_var))
 print(int+15)
 print(str_var+’15’) 
  1. In the first line, we have declared the number 15 as an integer.
  2. To verify its data type, we will apply the type function, it will display the output as- <class ‘int’>.
  3. The next step is to convert our data type from integer to string using the explicit type conversion and assign the string value to a new variable called ‘str_var’.
  4. On checking the data type, we will find that its datatype is an integer. (it will display the output as <class 'str’>.
  5. It’s time to check on what results in the variables int & str_var give when we perform operations on them.
  6. So, when we simply add the variable int with 15 print(int+15), it will display the output as 30.
  7. If we directly add 15 to the variable str_var, it will display an error because of the difference in data types, so we will write 15 as a string here
print(str_var+'15')
  • On running the given program, this print statement would give output as 1515.

Next, let’s have a look at how the same functionality can be performed by using f-string.

F-string in Python is a formatting mechanism that is also known as Literal String Interpolation.

The values declared as a string in Python can be embedded directly in the statement which we want to display using the print statement.

Let’s look at an example that illustrates the working of f-string.

 a='Python'
 b='Tutorial and Example'
 print(f"You will find different concepts of {a} at {b}") 

The f-string technique would replace the variables a and b with their respective values and would display them in the output of the print statement.

 int= 15                      #declaration of variable                                          
 print(type(int))                                               
 str_var=f'{int}'             #converting the data type
 print(type(str_var))                                       
 print(int+20)
 print(str_var+'20') 

On checking the data type, we'll find that the f-string technique converts the datatype of our variable and the output of print(type(str_var)) comes out to be <class ‘str’>.

The outputs that will be obtained after the execution of the last two print statements would be 35 and 1520.

Now, we will see how we can convert an integer to a string using "%s" keyword.

%s keyword performs the following functions-

  1. Performs concatenation of two strings.
  2. It is used to convert any value to a string data type.
  3. It formats a value inside a string.
 int=15                           #declaration of variable
 print(type(int))
 str_var="% s" % int              #converting the data type
 print(type(str_var))
 print(int+10)
 print(str_var+'25') 

On executing the given program, we'll observe that the %s keyword converts the integer data type to a string datatype. This can be verified by using print(type(str_var)).

Last is using. format() function,

Placeholders are used in a print statement when we are using the format function. In the parentheses, we provide the variables that replace these placeholders with their respective values.

Let’s try to understand this with the help of a simple example-

 a=10
 b=20
 add_num=a+b
 print("Sum of {} and {} is {}".format(a,b,add_num)) 

In the given program, we have declared the variables a and b as 10 and 20 respectively. After that to find the sum, we will store the value of a+b in a variable called add_num. Then in the print function, we will provide placeholders which will be replaced by the values of variables a,b, and add_num using the format function.

It can be used in our program in the following way-

 int=15                         #declaration of variable
 print(type(int))
 str_var="{}".format(int)        #converting the data type
 print(type(str_var))
 print(int+10)
 print(str_var+'25') 

The data types of both variables can be verified using the type function. On executing the given program, we will observe that the data type of int is an integer and the data type of str_var is a string.

So, in this article we discussed the methods of converting an integer data type to a string data type using str(), %s keyword, .format(), and f-string.


Related Topics

Python Selectors

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Python Struct

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

How To Take Multiple Inputs In Python

In C language, we make use of the scanf() function to obtain the values from the user and store it in the variable. Coming to the Python language, we use the...

5 minutes read.

Difference between Python 2 and Python 3

In this tutorial, we will learn the differences between two versions of python, that is, python version 2 and python version 3. Some basic differences include- Python 2 is the older version...

3 minutes read.

Mrcnn Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python Combination

Python Combination Permutations and combinations are the important part of our mathematical tools. We use them often in our Python programs. In this tutorial, we will learn about combinations in Python...

6 minutes read.

Python Program for Linear Search

Introduction We employ specific algorithms to efficiently carry out our responsibilities, such as searching for an element in a given data structure. These algorithms fall under the category of searching algorithms....

4 minutes read.

Defaultdict in Python

In this tutorial, we will study What is defaultdict in Python We will understand it with the aid of certain examples. Before this, we will have a look at dictionaries...

3 minutes read.

Gaussian elimination in python

Linear and polynomial equations are used in almost all fields of numerical simulation. However, its most common use in engineering is in the area of linear system analysis. The broader...

3 minutes read.

Python Encapsulation

What is meant by Encapsulation? The process of restricting access to the methods and variables so that accidental modification of data can be prevented is known as Encapsulation. The motive of Encapsulation:...

3 minutes read.

How to find square root in python

How to find Square Root of a number In Python Python makes a lot of tasks easier by using different functions, modules, and libraires. There is an inbuilt function in Python...

6 minutes read.

Python IDE

What is an IDE?  An IDE or an Integrated Development Environment is a type of software where developers can develop many software’s and applications and run the programs inside it. An...

11 minutes read.

Struct Module in Python

What is a structure in Python? An entity that holds data in form of a structure is called a data structure. In Python, the data structure includes tuples, lists, dictionaries, and...

4 minutes read.

List Comprehension in Python3

List A list in python is a complex data type, and a list is a collection of the number of data. The data variable may or may not be of the...

5 minutes read.

Python sklearn train_test_split

Sklearn: One of the most beneficial open-source libraries for learning algorithms in Python is, without a doubt, Sklearn or Scikit-Learn. The most effective tools for statistical modeling and machine learning are all included in...

6 minutes read.

Program of Cumulative Sum in Python

Introduction This tutorial, explains what is meant by lists, the cumulative total, several approaches to calculating the cumulative sum of digits in a list, etc. Learn the straightforward Python codes for...

5 minutes read.

Python Line Break

Introduction In this tutorial, you will learn line breaks in python.In Python, the new line character is used to indicate the start of a new line and the end of an...

5 minutes read.

How to run Python program in CMD

How to run python program in cmd Command Prompt provides us all together with a different approach for dealing with our programs. The programs can be executed by accessing the directories. In...

3 minutes read.

Python: SetBitmap() function in wxPython

SetBitmap() function In the last tutorial, we have discussed about the GetLabelText() function of wx.MenuItem class which is one of the important function of this class. Now, in this tutorial, we...

6 minutes read.

Python Program to Find the gcd of Two Numbers

Introduction Greatest Common Divisor is the full form of gcd. The greatest common divisor, or GCD, of two numbers, is a value that can exactly divide the two digits and is...

5 minutes read.