- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Convert Matrix to String
When it is required to convert a matrix into a string, a simple list comprehension along with the ‘join’ method can be used.
Example
Below is a demonstration of the same
my_list = [[1, 23, "python"], [1, "is", 24], ["fun", 97, 5]] print("The list is :") print(my_list) in_delete, out_delete = ",", " " my_result = out_delete.join([in_delete.join([str(element) for element in sub]) for sub in my_list]) print("The output string is :") print(my_result)
Output
The list is : [[1, 23, 'python'], [1, 'is', 24], ['fun', 97, 5]] The output string is : 1,23,python 1,is,24 fun,97,5
Explanation
A list of list is defined and is displayed on the console.
Two variables with symbols are defined.
A list comprehension is used to iterate through the elements in the list.
The elements are joined based on a specific condition.
This is assigned to a variable.
This variable is displayed as output on the console.
- Related Articles
- Python – Convert Integer Matrix to String Matrix
- Python - To Convert Matrix to String
- Python Program – Convert String to matrix having K characters per row
- Python program to Convert Matrix to Dictionary Value List
- Python – Convert String to matrix having K characters per row
- Python program to convert a list to string
- Python program to convert hex string to decimal
- Convert a list to string in Python program
- Program to convert given Matrix to a Diagonal Matrix in C++
- Program to convert a string to zigzag string of line count k in python
- Java Program to Convert Character to String
- Java Program to Convert String to Date
- Java Program to Convert InputStream to String
- Java Program to Convert OutputStream to String
- C# Program to Convert Integer to String

Advertisements