简体   繁体   中英

How To Print Iterated String together(without new lines) in Python?

I Want the output to be xyz but its coming like
x
y
z
as I am iterating over my string in for loop but Still any Way to print string together after iterating? My Simple Py Code =>

string='xyz'
for lel in string:
                        print lel

You mean this?

string = "xyz"
for letter in string:
    print letter,

Update: According to your comment, you can do some other things:

a) Use sys.stdout

for letter in string:
    sys.stdout.write(letter)

b) Use the print function:

from __future__ import print_function
for letter in string:
    print(letter, end='')

More about in this SO answer: How do I keep Python print from adding newlines or spaces?

Well I Was really being Dumb!! I Just Fixed it up :P Thnx For The Help everyone :)

string='xyz'
x=''
for lel in string:
                  x+=lel
print x
>>> a
'xyz'
>>> for i in a: sys.stdout.write(i)
xyz

You could use:

import sys
text = "xyz"
for l in text:
    sys.stdout.write(l)

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