Home > Enterprise >  How to do this without double operators?
How to do this without double operators?

Time:06-11

I was trying to print some numbers within a certain range horizontally using for loop with double operators( =)

str_1=''
for i in range(10): 
    str_1  = str(i) " " 
print(str_1)

The output shows as :

0 1 2 3 4 5 6 7 8 9

How to print this output without double operators( =)?

CodePudding user response:

Use the end kwarg:

for i in range(10):
    print(i, end=' ')
0 1 2 3 4 5 6 7 8 9

CodePudding user response:

You can use the join function

print(" ".join([str(i) for i in range(10)]))

CodePudding user response:

You can write one line:

>>> ' '.join(map(str,range(10)))
'0 1 2 3 4 5 6 7 8 9'

# OR
>>> " ".join(map("{}".format, range(10)))
'0 1 2 3 4 5 6 7 8 9'
  • Related