Python Program to convert List of Integer to List of String


In Python, a list is a collection of items or elements that can store multiple values. It is one of the built-in data types in Python and is commonly used for storing and organizing data.

A list is represented by square brackets [], and the elements within the list are separated by commas. The elements can be of any data type, including numbers, strings, Booleans, or even other lists. Lists are mutable, meaning you can modify, add, or remove elements from them.

Converting a list of integers to a list of strings involves converting each integer element into its string representation. In this article, we will see different approaches to converting a list of integers to a list of strings using Python programming.

Input Output Scenarios

Let's go through some input-output scenarios to understand the process of converting a list of integers to a list of strings.

Input: [1, 2, 3, 4, 5]
Output: ['1', '2', '3', '4', '5']

Let's explore the different approaches.

Using a for loop

This approach utilizes a for loop to iterate through the integers in the list. Within the loop, each integer is converted to a string using the str() function, and the resulting string is appended to a new list.

Example

Here's an example using the for loop to convert a list of integers to a list of strings.

# Define the input list
integer_list = [9, 3, 0, 1, 6, 4, 9]
print('Input list of integers:', integer_list)

# Convert using a for loop
string_list = []
for num in integer_list:
    string_list.append(str(num))
    
# Display the output
print('Output list of strings:', string_list)

Output

Input list of integers: [9, 3, 0, 1, 6, 4, 9]
Output list of strings: ['9', '3', '0', '1', '6', '4', '9']

Using a List Comprehension

This approach is similar to the previous one, but it offers the convenience of using a list comprehension. The list comprehension allows for a more concise and simplified code to convert the integers to strings and generate the new list.

Example

Here's an example using list comprehension.

# Define the input list
integer_list = [1, 2, 3, 4, 5]
print('Input list of integers:', integer_list)

# Convert using the List Comprehension
string_list = [str(num) for num in integer_list]

# Display the output
print('Output list of strings:', string_list)

Output

Input list of integers: [1, 2, 3, 4, 5]
Output list of strings: ['1', '2', '3', '4', '5']

Using the map() function

The map() function can be used to apply the str() function to each integer in the list. The map() function returns an iterator, so it is necessary to convert it to a list using list().

Example

Here's an example using the map() function.

# Define the input list
integer_list = [4, 3, 5, 4, 8, 9, 4]
print('Input list of integers:', integer_list)

# Convert using the map() function
string_list = list(map(str, integer_list))

# Display the output
print('Output list of strings:', string_list)

Output

Input list of integers: [4, 3, 5, 4, 8, 9, 4]
Output list of strings: ['4', '3', '5', '4', '8', '9', '4']

Using the format() function

The Python format() function allows for value formatting into strings with various formatting options. When the format() function is used with the 'd' format specifier, it is specifically intended for formatting integer values.

Example

Here's an example using the format() with the 'd' format specifier.

# Define the input list
integer_list = [6, 7, 0, 1, 6, 2, 4]
print('Input list of integers:', integer_list)

# Convert using the format() function
string_list = [format(num, 'd') for num in integer_list]
    
# Display the output
print('Output list of strings:', string_list)

Output

Input list of integers: [6, 7, 0, 1, 6, 2, 4]
Output list of strings: ['6', '7', '0', '1', '6', '2', '4']

These are the few different approaches to converting a list of integers to a list of strings using Python programming.

Updated on: 29-Aug-2023

163 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements