How to Convert List of ints to String in Python : 5 Methods

A list allows you to store multiple items in a single variable. To store extra items you will have to use the append() function provided by the list. You can convert the existing list of ints to string type in python easily using various methods. In this post, you will know all about it.

Method to convert python list of ints to string

Lets explore all the methods to change the type of the elements of the list.

Method 1: For loop

In this method, you will first create an empty list. After that iterate, all the elements of the list, and for each iteration append the elements to the empty list after the conversion of the type of the element.


#for loop
my_list = [1, 2, 3, 4, 5]
final_string= []
for i in my_list:
  final_string.append(str(i))
print(final_string)
print(type(final_string[0]))

Output

python ints of list to string using for loop
python ints of the list to string using for loop

Method 2: List Comprehension

List comprehension allows you to write the loop in a single statement. Its result is stored in a variable. Here you will iterate the list and with each iteration convert the type of the element to the string.

# list comphrension
my_list = [1, 2, 3, 4, 5]
final_string = [str(i) for i in my_list]
print(final_string)
print(type(final_string[0]))

Output

python ints of list to string using list comprehension
python ints of list to string using list comprehension

 

Method 3: Using str() function

You can directly convert the element of the integer type to the string using the str() function. Just pass the list to the str() function.

#str
my_list = [1, 2, 3, 4, 5]
my_string = str(my_list)
print(my_string)
print(type(my_string[0]))

Output

python ints of list to string using str function
python ints of list to string using str function

Method 4: Convert the python list of ints to string using the map function

Python programming language provides a map() function that accepts one argument as a function and the other as the input value (list). Also, you have to wrap the map() function with the list() to convert the result to list type.

# map
my_list = [1, 2, 3, 4, 5]
final_string = list(map(str,my_list))
print(final_string)
print(type(final_string[0]))

Output

python ints of list to string using map function
list of ints  to string using map function

 

Method 5: Using Enumerate

Enumerate adds a counter to the data object and makes that object iterable. To convert integer elements to the string element you have to first create an empty list and after each iteration using the enumerate append the converted element to the list. The final list will contain elements of the string type.

#enumerate
my_list = [1, 2, 3, 4, 5]
final_string= []
for i,a in enumerate(my_list):
  final_string.append(str(a))
print(final_string)
print(type(final_string[0]))

Output

python ints of list to string using enumerate
ints list to string using enumerate

Conclusion

These are the methods to modify all the elements of the list from integer to string. You can use it for loop, list comprehension, map function, and e.t.c to complete this task. You can choose any of them according to your convenience.

If you want to add any other methods here then you can contact us.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link