Python – Convert List of integers into List of strings

Python – Convert List of integers into List of strings

To convert a given list of integers into a list of strings in Python, you can use List comprehension or For loop to iterate over the elements in original list, and then use str() built-in function to convert each integer element into a string element.

Now, we shall go through some examples of converting a given list of integers into list of strings using list comprehension, or a For loop.

1. Converting list of integers into list of strings using List comprehension and str()

Consider that we are given a list of integer elements, and we have to convert them into a list of strings using List comprehension and str() builtin function.

Steps

  1. Given a list of integer values in my_list.
  2. Use list comprehension, and for each element in the original list, apply str() function to the element.
[str(x) for x in my_list]
  1. The above expression returns a list of strings created from the original list. Assign it to a variable, say list_str.
  2. You may print the resulting list of strings to the standard output using print() statement.

Program

The complete program to convert the given list of integers into a list of strings using List comprehension and str() built-in function.

Python Program

# Given a list of integers
my_list = [10, 20, 30, 40]

# Convert list of integers into list of strings
list_str = [str(x) for x in my_list]

print(list_str)
Run Code Copy

Output

['10', '20', '30', '40']

2. Converting list of integers into list of strings using For loop, list append() and str()

Consider that we are given a list of integer elements, and we have to convert them into a list of strings using For loop, list append() method, and str() built-in function.

Steps

  1. Given a list of integers in my_list.
  2. Take an empty list to store the resulting list of strings, say list_str.
  3. Use a For loop to iterate over the given list, and for each element in the given list, convert the integer element into a string element, and append the string element to list_str using list append() method.
for x in my_list:
  list_str.append(str(x))
  1. After the For loop is done, list_str contains the respective string values created from the original list.

Program

The complete program to convert the given list of integers into a list of strings using For loop, list append() function, and str() built-in function.

Python Program

# Given a list of integers
my_list = [10, 20, 30, 40]

# Take an empty list to store the strings
list_str = []

# Convert list of integers into list of strings
for x in my_list:
    list_str.append(str(x))

print(list_str)
Run Code Copy

Output

['10', '20', '30', '40']

Summary

In this tutorial, we have seen how to convert a given list of integers into a list of strings, in two approaches, with examples. The first one was using List comprehension and str() built-in function. The second approach was using For loop, list append() method, and str() built-in function.

Related Tutorials

Code copied to clipboard successfully 👍