w3resource

Python: Convert a list of integers, tuple of integers in a list of strings


8. Convert Numbers to Strings Map

Write a Python program to convert a given list of integers and a tuple of integers into a list of strings.

Sample Solution:

Python Code :

# Create a list named 'nums_list' and a tuple named 'nums_tuple' with integer elements
nums_list = [1, 2, 3, 4]
nums_tuple = (0, 1, 2, 3)

# Print the original list and tuple
print("Original list and tuple:")
print(nums_list)
print(nums_tuple)

# Use the map function to convert each element in 'nums_list' to a string and create a new list
result_list = list(map(str, nums_list))

# Use the map function to convert each element in 'nums_tuple' to a string and create a new tuple
result_tuple = tuple(map(str, nums_tuple))

# Print a message indicating the operation to be performed
print("\nList of strings:")

# Print the result of the map operation for the list as a list of strings
print(result_list)

# Print a message indicating the operation to be performed
print("\nTuple of strings:")

# Print the result of the map operation for the tuple as a tuple of strings
print(result_tuple)

Sample Output:

Original list and tuple:
[1, 2, 3, 4]
(0, 1, 2, 3)

List of strings:
['1', '2', '3', '4']

Tuple of strings:
('0', '1', '2', '3')

For more Practice: Solve these Related Problems:

  • Write a Python program to convert each integer from a list and a tuple into its string representation using map, then merge them into one list.
  • Write a Python program to use map to convert numbers from a list and a tuple into their binary string format.
  • Write a Python program to convert each integer from a list and tuple into a hexadecimal string using map, then combine the results.
  • Write a Python program to map a lambda that converts numbers to strings with leading zeros from a list and a tuple, then concatenate the outputs.

Go to:


Previous: Write a Python program to add two given lists and find the difference between lists. Use map() function.
Next: Write a Python program to create a new list taking specific elements from a tuple and convert a string value to integer.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.