0

Is it possible to print text on the same line in Python?

For instance, instead of having

1
2
3

I would have

1 2 3

Thanks in advance!

Zizouz212
  • 4,908
  • 5
  • 42
  • 66
Alex Carr
  • 39
  • 1
  • 1
  • 6
  • 2
    So you want the exact *opposite* of printing on the next line? What have you tried? There are dozens of questions related to this already. – jonrsharpe Nov 24 '15 at 22:26
  • This might help: http://stackoverflow.com/questions/3249524/print-in-one-line-dynamically – b han Nov 24 '15 at 22:28
  • @michelpri not exactly the same as what has been asked. What you link does is overwriting a line that has been printed – Ricardo Cárdenes Nov 24 '15 at 22:29

3 Answers3

5

Assuming this is Python 2...

print '1',
print '2',
print '3'

Will output...

1 2 3

The comma (,) tells Python to not print a new line.

Otherwise, if this is Python 3, use the end argument in the print function.

for i in (1, 2, 3):
    print(str(i), end=' ') # change end from '\n' (newline) to a space.

Will output...

1 2 3
Zizouz212
  • 4,908
  • 5
  • 42
  • 66
1

when putting a separator (comma) your problem is easily fixed. But obviously I'm over four years too late.

print(1, 2, 3)

No Bounds
  • 43
  • 6
1

Removing the newline character from print function.

Because the print function automatically adds a newline character to the string that is passed, you would need to remove it by using the "end=" at the end of a string.

SAMPLE BELOW:

print('Hello')
print('World')

Result below.

Hello
World

SAMPLE Of "end=" being used to remove the "newline" character.

print('Hello', end= ' ')
print('World')

Result below.

Hello World

By adding the ' ' two single quotation marks you create the space between the two words, Hello and World, (Hello' 'World') - I believe this is called a "blank string".