Using python I want to print a range of numbers on the same line. how can I do this using python, I can do it using C by not adding \n
, but how can I do it using python.
for x in xrange(1,10):
print x
I am trying to get this result.
1 2 3 4 5 6 7 8 9 10
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.
When you wish to print the list elements in a single line with the spaces in between, you can make use of the "*" operator for the same. Using this operator, you can print all the elements of the list in a new separate line with spaces in between every element using sep attribute such as sep=”/n” or sep=”,”.
To print multiple expressions to the same line, you can end the print statement in Python 2 with a comma ( , ). You can set the end argument to a whitespace character string to print to the same line in Python 3. With Python 3, you do have the added flexibility of changing the end argument to print on the same line.
The Python range() function returns the sequence of the given number between the given range. The most common use of it is to iterate sequence type (Python range() List, string, etc. )
>>>print(*range(1,11)) 1 2 3 4 5 6 7 8 9 10
Python one liner to print the range
Python 2
for x in xrange(1,11):
print x,
Python 3
for x in range(1,11):
print(x, end=" ")
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