Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Best way to convert multidimensional array to string

I have a Python multidimensional array:

list = [[0,1,2],[3,4,5],[6,7,8]]

Is there any way to convert it to a string like this one?

# it must keep the brackets
string = "[[0,1,2],[3,4,5],[6,7,8]]"

Of course, I could loop through the array and build my string, but I wanted to know if there’s any other better option.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any help is appreciated.

>Solution :

Simply you can use str method.

l = [[0,1,2],[3,4,5],[6,7,8]]
s = str(s)
print(s) # [[0,1,2],[3,4,5],[6,7,8]]
print(type(s)) # <class 'str'>
print(s[0]) # [
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading