-1

I know how to type numbers from python.

>>> for  a in range(1,11):
     print(a)

1
2
3
4
5
6
7
8
9
10

Here the output is given in one line after the other. So I want to type the numbers in the same line without using lists and stacks. I that possible? Then how can I do that?

awesoon
  • 32,469
  • 11
  • 74
  • 99

2 Answers2

3

print automatically adds a newline character after the string you've entered, this is why it prints each number on a different line. To change this behavior, you must change end parameter on the function call.

for a in range(1,11):
    print(a, end=' ')

Edit: The end parameter holds a string which gets printed after the string you've entered. By default it's set to \n so after each print, \n is added:

print("Hello!") # actually prints "Hello!\n"

You can change the parameter to anything you like:

print("Hello!", end="...") # prints "Hello!..."
print("Hello!", end="") # prints "Hello!"
  • Thanks for th answer.It seems as an easy way which does my job.Actually what does end do here?I'm a beginner to Python.. – Sasanka Weerathunge Apr 09 '13 at 02:06
  • @SasankaWeerathunge It defines what to print after the actual string you entered. See my latest edit. –  Apr 09 '13 at 11:32
0

Try this:

>>> print(' '.join(str(i) for i in range(1,11)))
1 2 3 4 5 6 7 8 9 10
Roland Smith
  • 42,427
  • 3
  • 64
  • 94