×

How to Convert Int to String in Python?

Every value we use or store in a variable in Python will have a specific data type. It describes the value's nature; based on that, Python will automatically assign a data type. In this article, we will see int-to-string conversion techniques that change an inputted integer value into a string.

Example:

Python supports a variety of data structures, including lists, tuples, dictionaries, and sets, as well as numerous data types for various value sets. Integers deal with numbers and strings that may accept alphanumeric values. To give you an overview of the Python data types, we defined two different values in the example below and tested their data types.

x='It's a Beautiful World.'
y=85
print(type(x))
print(type(y))

Output:

<class  ‘str’>
<class ‘int’>

We could infer from the example that the string data type and integer data types are quite clear. A sentence enclosed in quotations has been assigned to the variable "x." The variable "y" is a straightforward numeric character and illustrates a string data type.

How does Python Convert an Integer to a String?

There are four ways to convert an integer data type in Python to a string data type. They are listed below:

  • Using the method str()
  • With %s string
  • .format() function use
  • F-string usage

1. The str() method

Syntax:

str(integer)

The str() function translates the value supplied in and returns a data type of string. The object can be a string, an int, or a char. If no parameter is supplied, the function returns nothing.

Example:

Let's look at some basic code that converts an int to a string.

#int to string conversion
x = 10
# print out the x variable's data type.
print(type(x))
# make the x variable a string
y = str(x)
# determine and report the y variable's type.
print(type(y))

Output:

<class  ‘str’>
<class ‘int’>

In the procedure above, we have assigned the variable 'x' an integer value of 10, and converted it using Python's str() function, and the resulting 'y' variables returned a string data type.

Example:

The string data type supports various operations, including cutting, replacing, concatenating, and more. Let's talk about a different instance, as illustrated below.

#string value printing code
a = 'Its a beautiful day
print(a[1])
print(a[2:7])

Output:

t
s a b

We have assigned the string "It's a gorgeous day" to the variable in the code above. The sliced first position of the string, which is also a string, is printed in the next line. The string's second and seventh positions have been cut off and printed in the next line. The string data type is simple to use and offers a variety of characteristics.

2. Using %s, convert an integer to a string

Syntax:

"%s" % integer value

Example:

# int to string conversion using %s
a = 5
# print the value and data type of variable a
print(a)
print(type(a))
# convert the integer a into a string and print the value and data type
b = "% s" % a
print(b)
print(type(b))

Output:

5
<class  ‘int’>
5
<class ‘str’>

In the above program, we've given the variable an integer value of 5, and we've printed the value and data type of a. Using the, we changed the integer a to a string b in the following section. In the final step, we printed the b's data type

and verified the outcome. We'll add two integer numbers and transform the result into a string in a subsequent example.

Example:

5
<class  ‘int’>
5
<class ‘str’>

Output:

<class  ‘int’>
<class ‘str’>

We are adding two int values, assigning the result to c, and outputting the data type, as in the previous example. The following step involves checking the data type while converting an int to a string.

3. Converting Int to String Using f- String

Syntax:

f’{integer_value}’

Example:

# f-string is used to convert ints to strings.
a = 5
print(type(a))
b = of{a}'
print(type(b))

Output:

<class  ‘int’>
<class ‘str’>

4. Using. Format, convert an integer to a string () Method

Syntax:

‘{}’.format(integer_value)
#Using the. Format () method, convert an int to a string.
name = "Bala"
age = 26
# using the Method. format()
print("{name} is {age} years old".format(name=name, age=age))

Output:

Bala is 26 years old

The string "Bala" has been allocated to the name variable in the code above, and the integer value of 26 has been assigned to the age variable. The age variable will be converted to a string using the. Format () method in the following step, and the string will then be printed as displayed in the output.

Example:

In the example below, we have received two string values as input, added them, and then printed the result's data type.

#code to sum of two input strings and convert it to the int value
x = input('Enter first integer value:\n')
x=int(x)
y = input('Enter second integer value:\n')
y=int(y)
sum_inputs=x+y
print('sum of Inputs:{}'.format(sum_inputs))
print(type(sum_inputs))

Output:

Enter first integer:
16
Enter second integer:
33
Sum:
49
<class ‘int’>

In the above code, we have declared the two inputs, x and y, to be strings, then summed them, converted them to integer values, and displayed the total as output. If we performed an addition operation without converting the numbers into an int, the output would be as we would expect.

Enter first integer:
111
Enter second integer:
33
Sum:
11133
<class ‘str’>

Since we declared two string values in the output above and performed a sum operation on them because they are string values, the result is merely a combination of the two strings; no mathematical action is carried out because it is a string value.


Related Topics

What is the re.sub() function in Python

There. sub () function is used to return a string by replacing the occurrences of a specific character or pattern with a replacement string. To use this function, import the...

3 minutes read.

Python pycountry

What is Pycountry? Python programming language: 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...

4 minutes read.

Looping through Data Frame in Python

Iterate over Rows and Columns in Pandas Dataframe This tutorial aims to make us understand what Pandas in Python are, what Data Frame in Python is and its significance, what are...

4 minutes read.

Python program to find factorial of a given number

Python program to find factorial of a given number Factorial is a non-negative integer. It is the product of all positive integers from 1 to the number we are going to...

3 minutes read.

Python Set update() method

Python Set update() method The set.update() method in Python updates the current set, by adding items from another set. If an element is common in both the sets, only one appearance of this item...

1 minute read.

Calculator Program in Python

Simple Calculator Program in Python This example helps to learn how to create a simple calculator program to perform operations like addition, subtraction, multiplication, and division. Source Code The following is the source...

2 minutes read.

Python program to find Fibonacci series

Python program to find Fibonacci series A Fibonacci series is an integer sequence of 0, 1, 1, 2, 3, 5, 8.... We can identify the Fibonacci series as any number sequence...

2 minutes read.

Conditional Statements in python

In this article, we will learn about conditional statements and how to apply conditional statements in Python. A decision-making statement is another name for a conditional statement. Decision-making is an important...

5 minutes read.

Best Python AI Projects

Artificial consciousness is advancing quickly, from Chabot's to self-driving vehicles. Because of the various advantages and development presented by AI, numerous enterprises have begun searching for AI-fueled applications. Thus, there...

7 minutes read.

Reverse a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Writing to a CSV file in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

6 minutes read.

Python program to convert Celsius into Fahrenheit

Python program to convert Celsius into Fahrenheit This program explains how we can take the temperature in Celsius and convert them into Fahrenheit. Celsius Celsius, also known as centigrade, is a measurement unit...

1 minute read.

Python | Read csv using pandas.read_csv()

Python is an excellent language for performing information analysis, owing to the fantastic biological system of information-driven python packages. Pandas is one of those packages that make taking in and...

4 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 Configure Python Interpreter in Eclipse

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.

Excel to CSV in Python

Define MS Excel Microsoft Excel is an application provided by the Microsoft corporation which allows us to build graphs to build tables, and it is also used for the macro programming...

4 minutes read.

Python next() function

Python next() function The next() function in Python retrieves the next item from the iterator.  Syntax next(iterator[, default]) Parameter iterable: It is a required parameter which represents an iterable object. default: This parameter represents a default value to...

1 minute read.

Permutations in Python

Recursion Basic idea: for numbers of length N. N-1 items are chosen at random between 0 and then generate permutations using the remaining N-1 elements in a recursive fashion. Once you've done...

4 minutes read.

What are the Purposes of Python?

Python is a high-level programming language that is simple to use and easy to write, and Python is a beginner-friendly programming language. A beginner often prefers to start with Python...

6 minutes read.

Face Recognition in Python

In this tutorial, we will understand what is face recognition and how it is achieved in python. Face recognition is of great utility in real-world scenarios. It is an extended step...

3 minutes read.