How To Print On The Same Line In Python
Solution 1:
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
Solution 2:
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".
Solution 3:
when putting a separator (comma) your problem is easily fixed. But obviously I'm over four years too late.
print(1, 2, 3)
Post a Comment for "How To Print On The Same Line In Python"