-2

I've got a function in Python

def wordPrint():
    print "word"

I want to use it to print 3 times on the same line using a for loop.

I would expect this code

for x in range(0,3):
    wordPrint(),

to output this

word word word

But I get them on new lines

Tomulent
  • 531
  • 2
  • 5
  • 20
  • 1
    You can't. `wordPrint` *always* adds a newline. Well, you could do something hacky, but it's probably best to change your approach altogether. – juanpa.arrivillaga Feb 16 '18 at 01:10
  • Use `os.stdout.write("word\r")` (or something like that). Other comments are correct, `print will always add a newline. Use `os.stdout.write()` which does not add the newline. The `\r` should return you back to the start of the current line. – marklap Feb 16 '18 at 01:23

1 Answers1

0

print() always prints on a new line. You'll have to use one print statement if you want one line of output. Using a for loop, you could do:

word = ""
for x in range(0,3):
    word += "word "
print(word)

Or, you could use the * operator (which is more pythonic).

print("word " * 3)
Eli Miller
  • 11
  • 3