Convert List from Integer to String in Python (2 Examples)

 

On this page you’ll learn how to convert lists of integers to lists of strings in the Python programming language.

The tutorial will contain the following contents:

If you want to know more about these contents, please keep reading 🙂

 

Example Data

We’ll use a sample of a list of integers for the demonstration in this tutorial.

sl_int=[3, 2, 4, -20, -5, 120]      # create sample data
print(sl_int)                       # print sample data
 
# [3, 2, 4, -20, -5, 120]

In the previous code, a sample list called sl_int was created and printed out. It contains six different numbers.

Now it is time to check the data type of the elements in the list sl_int. For this, we can use the print and type functions within a for loop.

for element in sl_int:             # print sample data types 
  print(type(element))              
 
# <class 'int'>
# <class 'int'>
# <class 'int'>
# <class 'int'>
# <class 'int'>
# <class 'int'>

As you can see, the data types of all elements are integers. In the following sections, I will show how to convert the data types to strings.

 

Example 1: Transform List of Integers to Strings Using list() & map() Functions

The following code demonstrates how to convert lists of integers to lists of strings via list() and map() functions.

sl_str1=list(map(str, sl_int))      # apply list() & map() functions
print(sl_str1)                      # print output of list() & map()
 
# ['3', '2', '4', '-20', '-5', '120']

The implementation has taken place above. Now it is time to check if the transformation was successful. To achieve this, we may once again use the print and type functions within a for loop.

for element in sl_str1:
  print(type(element))               # print converted data types 
 
# <class 'str'>
# <class 'str'>
# <class 'str'>
# <class 'str'>
# <class 'str'>
# <class 'str'>

As seen, now all the elements in sl_str1 are in string type.

 

Example 2: Transform List of Integers to Strings Using List Comprehension

In this example, I’ll explain how to use list comprehensions for the conversion of lists of integers to lists of strings.

Have a look at the following Python syntax and its output:

sl_str2=[str(x) for x in sl_int]    # list comprehension method
print(sl_str2)                      # print output of list comprehension
 
# ['3', '2', '4', '-20', '-5', '120']

The use of list comprehensions is shown above. Now it is time to check if the implementation was successful.

for element in sl_str2:
  print(type(element)) # print converted data type
 
# <class 'str'>
# <class 'str'>
# <class 'str'>
# <class 'str'>
# <class 'str'>
# <class 'str'>

Well done, looks like we handled it well.

 

Video & Further Resources

Have a look at the following video on my YouTube channel. In the video, I illustrate the Python programming codes of this tutorial:

 

The YouTube video will be added soon.

 

In addition, you might have a look at the related tutorials on this website. Some interesting articles about topics such as data conversion and character strings are shown below.

 

Summary: You have learned in this tutorial how to transform a list of integers to strings in the Python programming language. In case you have additional questions, let me know in the comments section.

 

Cansu Kebabci R Programmer & Data Scientist

This page was created in collaboration with Cansu Kebabci. Have a look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview on her other tasks on Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top