简体   繁体   English

如何在Python中一起打印迭代字符串(没有新行)?

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

I Want the output to be xyz but its coming like 我希望输出为xyz,但它的输出像
x X
y ÿ
z ž
as I am iterating over my string in for loop but Still any Way to print string together after iterating? 当我在for循环中遍历我的字符串时,但是在迭代后仍然有任何方法可以一起打印字符串? My Simple Py Code => 我的简单Py代码=>

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 a)使用sys.stdout

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

b) Use the print function: b)使用打印功能:

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? 有关此SO答案的更多信息: 如何防止Python打印添加换行符或空格?

Well I Was really being Dumb!! 好吧,我真的很笨! I Just Fixed it up :P Thnx For The Help everyone :) 我只是把它修好了:P Thnx对于所有人的帮助:)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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