0

Then How I get the Same effect of stripe method but on integers this is my code to generate out but like

1
121
12321
1234321
123454321

but the result was : i want to remove spaces between numbers in one line code after for statement

for i in range(1,int(input())+1): 
    print(*range(1,i+1),*range(1,i).__reversed__())
  • Did you check [the docs](https://docs.python.org/3/library/functions.html#print) for `print()`, about `sep` parameter? – buran Jun 16 '22 at 07:12
  • Use sep keyword in print function: `print(*range(1,i+1),*range(1,i).__reversed__(), sep = '')` – DarrylG Jun 16 '22 at 07:12
  • 1
    Does this answer your question? [What does print(... sep='', '\t' ) mean?](https://stackoverflow.com/questions/22116482/what-does-print-sep-t-mean) – buran Jun 16 '22 at 07:13

2 Answers2

1

Python's print has an optional sep parameter.

for i in range(1, int(input()) + 1): 
    print(*range(1, i + 1), *range(1, i).__reversed__(), sep='')

Though that __reversed__() call is ugly.

for i in range(1, int(input()) + 1): 
    print(*range(1, i + 1), *range(i - 1, 0, -1), sep='')

Please note there is also an optional end parameter. You may, for instance, not want your print to automatically skip to the next line.

for i in range(1, int(input()) + 1): 
    print(*range(1, i + 1), sep='', end='')
    print(*range(i - 1, 0, -1), sep='')
Chris
  • 26,361
  • 5
  • 21
  • 42
0

Try formatting a string first and pass that to print.

for i in range(1,int(input())+1): 
    line = ''.join(map(str, range(1, i+1)))
    line += ''.join(map(str, reversed(range(1, i))))
    print(line)
James
  • 32,991
  • 4
  • 47
  • 70