-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
  • 30,028
  • 9
  • 67
  • 92

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!"
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
  • 39,308
  • 3
  • 57
  • 86