-1

Hey I've got a problem with the for loop, so let say I want to achieve printing "#" signs 5 times with for loop with out space and in one line.

for i in range(5):
    print "#",

can how I get ride of the space in between so it looks like ##### instead of # # # # #???

  • oh hey guys, the thing is that I try to not use the multiplcation(*) operator, or any repeat concatenation , and I have to use the for loop to print it out. – user2020817 Feb 10 '13 at 01:44
  • thanks everyone for help I've just found out how to do it!! thanks a lot. appreciate all the suggestion!! – user2020817 Feb 10 '13 at 01:46
  • 1
    ***Why*** are you trying to avoid these things? And ***why*** must you use a `for`-loop? If you explain your constraints in the question, you're more likely to get an answer that is suitable. Anyway, the answers for [How do I keep Python print from adding spaces?](http://stackoverflow.com/questions/255147/how-do-i-keep-python-print-from-adding-spaces) are still valid. – johnsyweb Feb 10 '13 at 01:46
  • 1
    Are you saying you want an alternative to doing the easiest thing? Like homework? – sotapme Feb 10 '13 at 01:48
  • oh its for a lab exercise, it has restrictions, and since we are learning to use for loops, the question asks us to not use multiplication operator or any repeat concatenation, but to use for loop only to achieve the result. Sorry for not clarifying this in the question. – user2020817 Feb 10 '13 at 01:56

5 Answers5

3

I assume you plan on doing things in this loop other than just printing '#', if that is the case you have a few options options:

import sys
for i in range(5):
    sys.stdout.write('#')

Or if you are determined to use print:

for i in range(5):
    print "\b#",

or:

from __future__ import print_function
for i in range(5):
    print('#', end='')

If you actually just want to print '#' n times then you can do:

n = 5
print '#'*n

or:

n = 5
print ''.join('#' for _ in range(n))
Matt
  • 3,651
  • 3
  • 16
  • 35
0

Use the print function instead. (Note that in Python 2 print is normally a statement, but by importing print_function, it turns into a function).

from __future__ import print_function

for i in range(5):
    print('#', end='')

The function supports an end keyword argument, which you can specify.

Volatility
  • 31,232
  • 10
  • 80
  • 89
  • To be clear, the distinction is that `print` is normally a keyword (Python 2.7 only I think), so this is a way of using a print *function*. – Ben Mordecai Feb 10 '13 at 01:30
0

I would use a join... something like this should work:

print(''.join('x' for x in range(5)))
David S
  • 12,967
  • 12
  • 55
  • 93
0

You can do it without a loop!

print '#' * 5

If you're making something more complicated, you can use str.join with a comprehension:

print ''.join(str(i**2) for i in range(4)) # prints 0149
print ','.join(str(i+1) for i in range(5)) # prints 1,2,3,4,5
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • I thought the OP didn't want a comma. But, your join is absolutely correct if he does. – David S Feb 10 '13 at 01:25
  • It was an example. But, I guess it is clearer without the comma. Thanks. – nneonneo Feb 10 '13 at 01:26
  • Yeah; but, your solution with the colums is really good for someone that needs it to be more complicated. I liked it. :) – David S Feb 10 '13 at 01:27
  • Boy... people really jumped all over this questions. Lots of loops :) – David S Feb 10 '13 at 01:29
  • OK, compromise. Commas *and* no commas! – nneonneo Feb 10 '13 at 01:33
  • @DavidS When I read the question I assumed that the OP wanted to do things other than just printing in his loop and those other things were removed from his example as they were immaterial to the question – Matt Feb 10 '13 at 01:35
0

In addition to the other answers posted, you could also:

mystring = ''
for i in range(5):
    mystring += '#' # Note that you could also use mystring.join() method
                    # .join() is actually more efficient.
print mystring

or you could even just:

print '#'*5
Ben Mordecai
  • 685
  • 2
  • 8
  • 19