I have the following code:
>>> x = 0
>>> y = 3
>>> while x < y:
... print '{0} / {1}, '.format(x+1, y)
... x += 1
Output:
1 / 3,
2 / 3,
3 / 3,
I want my output like:
1 / 3, 2 / 3, 3 / 3
I searched and found that the way to do this in a single line would be:
sys.stdout.write('{0} / {1}, '.format(x+1, y))
Is there another way of doing it? I don't exactly feel comfortable with sys.stdout.write()
since I have no idea how it is different from print
.
while x < y: ... print '{0} / {1}, '.
The Python's print() function is used to print the result or output to the screen. By default, it jumps to the newline to printing the next statement. It has a pre-defined format to print the output.
Modify print() method to print on the same line The print method takes an extra parameter end=” “ to keep the pointer on the same line. The end parameter can take certain values such as a space or some sign in the double quotes to separate the elements printed in the same line.
To print on the same line in Python, add a second argument, end=' ', to the print() function call. print("It's me.")
you can use
print "something",
(with a trailing comma, to not insert a newline), so try this
... print '{0} / {1}, '.format(x+1, y), #<= with a ,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With