简体   繁体   中英

Print all the list values in a single row or single line in python

I am using the below code in python to print the values in the list in a single row, but it prints everything in a new line.

for abc in values: 
   print ','.join(abc)

Desired output:

S,t,r,e,e,t
N,o
Y,e,s

I assume you are using python 2. If this is the case, you can add a trailing comma after your print statement:

>>> for abc in ['street','No', 'Yes']:
...     print ','.join(abc),
... 
s,t,r,e,e,t N,o Y,e,s

If you were using Python 3, you can then use the extra argument end in the print function:

>>> for abc in ['street','No', 'Yes']:
...     print(','.join(abc), end='')
... 
s,t,r,e,e,tN,oY,e,s>>> 

Note that this does not include a trailing new line.

The Python 3 method can be accomplished in Python 2 by inserting from __future__ import print_statement in the first line of your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM