Program to convert the integer elements of matrix into string matrix using Python

by | Mar 9, 2021 | Python Programs

Home » Python » Python Programs » Program to convert the integer elements of matrix into string matrix using Python

Introduction

The task is to convert and print the integer elements of matrix into string elements.

Program to convert the integer elements of matrix into string matrix

Program

input_matrix1 = [[16,0], [8,19]]
input_matrix2 = [[16,0], [8, 19]]
print("The original list : "+str(input_matrix1))


#Using str() and list comprehension
output1 = [[str(i) for i in ele] for ele in input_matrix1]


print("The data type converted Matrix : "+str(output1))


#Using str() and map()
output2 = [list(map(str, i)) for i in input_matrix2]
print("The data type converted Matrix : "+str(output2))

Output

Program to convert the integer elements of matrix into string matrix Output

Explanation

In the above python code, we have used two approaches. The first one is conversion using str() function and list comprehension. The list comprehension will iterate through the elements of matrix and str() function is used to convert the integer elements to string. The resultant values are stored in variable output1 and is displayed on screen using print function.

The second approach is using str() and map() functions. The map function will extend the range of area of application of str() function i.e. it will extend all the elements of matrix as a string.

Author

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author