Thursday, December 26, 2024
Google search engine
HomeLanguagesConvert integer to string in Python

Convert integer to string in Python

In Python an integer can be converted into a string using the built-in str() function. The str() function takes in any python data type and converts it into a string. But use of the str() is not the only way to do so. This type of conversion can also be done using the ā€œ%sā€ keyword, the .format function or using f-string function.

Below is the list of possible ways to convert an integer to string in python:

1. Using str() functionĀ 

Syntax: str(integer_value)

Example:Ā Ā 

Python3




num = 10
Ā 
# checkĀ  and print type of num variable
print("Type of variable before conversion : ", type(num))
Ā 
# convert the num into string
converted_num = str(num)
Ā 
# checkĀ  and print type converted_num variable
print("Type After conversion : ",type(converted_num))


Output:

Type of variable before conversion :  <class 'int'>
Type After conversion :  <class 'str'>

2. Using ā€œ%sā€ keyword

Syntax: ā€œ%sā€ % integer

Example:Ā 

Python3




num = 10
Ā 
# checkĀ  and print type of num variable
print("Type of variable before conversion : ", type(num))
Ā 
# convert the num into string and print
converted_num = "% s" % num
print("Type after conversion : ", type(converted_num))


Output:

Type of variable before conversion :  <class 'int'>
Type after conversion :  <class 'str'>

3. Using .format() function

Syntax: ā€˜{}ā€™.format(integer)

Example:Ā 

Python3




num = 10
Ā 
# checkĀ  and print type of num variable
print("Type before conversion : ", type(num))
Ā 
# convert the num into string and print
converted_num = "{}".format(num)
print("Type after conversion :",type(converted_num))


Output:

Type before conversion :  <class 'int'>
Type after conversion : <class 'str'>

4. Using f-string

Syntax: f'{integer}ā€™

Example:Ā 

Python3




num = 10
Ā 
# checkĀ  and print type of num variable
print("Type before conversion : ",type(num))
Ā 
# convert the num into string
converted_num = f'{num}'
Ā 
# print type of converted_num
print("Type after conversion : ", type(converted_num))


Output:

Type before conversion :  <class 'int'>
Type after conversion :  <class 'str'>

Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā Ā 5. Using __str__() methodĀ 

Syntax: Ā Integer.__str__()

Python3




num = 10
Ā 
# checkĀ  and print type of num variable
print("Type before conversion : ",type(num))
Ā 
# convert the num into string
converted_num = num.__str__()
Ā 
# print type of converted_num
print("Type after conversion : ", type(converted_num))


Output:

Type before conversion :  <class 'int'>
Type after conversion :  <class 'str'> 

Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  6. Using str.isdigit()Ā 

Python3




str_value = "1234"
if str_value.isdigit():
Ā Ā Ā Ā int_value = int(str_value)
Ā Ā Ā Ā print(int_value)
Ā Ā Ā Ā print(type(int_value))
else:
Ā Ā Ā Ā raise ValueError("Invalid literal for int(): {}".format(str_value))


Output

1234
<class 'int'>

7.Using join() method:

Ā join() method is used to convert a list of integers to a string. We convert the integer to a list of characters using the list() function and then join them using the join() method.

Python3




num = 10
Ā 
# checkĀ  and print type of num variable
print("Type before conversion : ",type(num))
Ā 
# convert the num into string
converted_num = ''.join(list(str(num)))
Ā 
# print type of converted_num
print("Type after conversion : ", type(converted_num))


Output

Type before conversion :  <class 'int'>
Type after conversion :  <class 'str'>

Time Complexity: O(N) where n is the number of digits in the integer.Ā 

Space Complexity:O(N) as we need to create a list of characters that has n elements,

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?