string - In Python 2, can I pass a list to the percent-format operator? -
I have a list of things I want to print using the format string. In Python 3-style, using "a string" .format (arg, arg, arg), it's easy. I can simply replace it with arguments with
* mylist
, like
mylist = [1,2,3] "{} {} {}". Format (* mylist))
I can not do this work with the old percentage formatting. I have tried stuff like the "% i% i% i"% mylist
, % i% i% i "% (mylist)
, and % I% i% i "% (* mylist)
but I keep syntax errors or" not enough logic ".
Is it impossible in 2.x style?
ETA: I'm changing the example above, I did not really mean list , tupal . I am still learning and I have not been used to produce two different array-like.
Should just work.
& gt; & Gt; & Gt; Mylist = (1, 2, 3) # & lt; ---- Tuple & gt; & Gt; & Gt; "% I% i% i"% mylist '1 2 3'
BTW, (1, 2, 3)
A Tupe is literal, not a list Verbal
& gt; & Gt; & Gt; Mylist = [1, 2, 3] # & lt; ----- List & gt; & Gt; & Gt; "% I% i% i"% mylist traceback (the most recent call final): File "& lt; stdin>", line 1, & lt; Module & gt; TypeError:% d format: A number is required, do not list
If you want to do it for the list, then convert the list to Tulle.
& gt; & Gt; & Gt; "% I% i% i"% tuple (mylist) '1 2 3'
Comments
Post a Comment