Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String representation of arrays in python

Tags:

python

Is there anything that performs the following, in python? Or will I have to implement it myself?

array = [0, 1, 2]
myString = SOME_FUNCTION_THAT_TAKES_AN_ARRAY_AS_INPUT(array)
print myString

which prints

(0, 1, 2)

Thanks

like image 567
devoured elysium Avatar asked Dec 05 '22 04:12

devoured elysium


1 Answers

You're in luck, Python has a function for this purpose exactly. It's called join.

print "(" + ", ".join(array) + ")"

If you're familiar with PHP, join is similar to implode. The ", " above is the element separator, and can be replaced with any string. For example,

print "123".join(['a','b','c'])

will print

a123b123c
like image 88
dwlz Avatar answered Dec 08 '22 05:12

dwlz