Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print range of numbers on same line

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
like image 766
kyle k Avatar asked Aug 25 '13 01:08

kyle k


People also ask

How do you print the range of a single line in Python?

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.

How do I print a list of elements 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=”,”.

How do I print multiple print statements on the same line?

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.

How do you show a range of numbers in Python?

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. )


2 Answers

>>>print(*range(1,11))  1 2 3 4 5 6 7 8 9 10 

Python one liner to print the range

like image 104
voidpro Avatar answered Sep 24 '22 11:09

voidpro


Python 2

for x in xrange(1,11):
    print x,

Python 3

for x in range(1,11):
    print(x, end=" ") 
like image 39
blakev Avatar answered Sep 21 '22 11:09

blakev