0

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

4 Answers4

2

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?

Community
  • 1
  • 1
Esparta Palma
  • 735
  • 5
  • 10
0

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
  • ... did you try: `print(string)`? Why do you think you have to rebuild the string? Also, if you have a list of strings like `['Hello', 'World']` you can use `join` to join the strings together: `', '.join(['Hello', 'World!']) -> 'Hello, World!'`. – Bakuriu Sep 17 '14 at 16:47
  • :P I Know i just asked this Coz i Want to add something to every Char in the string in the loop which i Didn't mention here , its Working now Thnx – user3881949 Sep 17 '14 at 16:49
0
>>> a
'xyz'
>>> for i in a: sys.stdout.write(i)
xyz
g4ur4v
  • 3,210
  • 5
  • 32
  • 57
0

You could use:

import sys
text = "xyz"
for l in text:
    sys.stdout.write(l)
cchristelis
  • 1,985
  • 1
  • 13
  • 17