How to remove the last "." from for loop print

Viewed 149
for i in range (20):
    print(i, end='.')

my result is actually

0.1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.

is there any way or advise so that my print will be

0.1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19
3 Answers

You can do this:

print(".".join(str(n) for n in range(20)))

That generates the numbers from 0 to 19 (inclusive), converts each one to a string, joins them together with . as a separator, then prints the result.

I would prefer to use the inline "if" statement. for i in range(20): print(i, end="" if i == 19 else ".")

You can get all elements but the last one. Let's say you have that string on the variable my_str, you could try:

my_str[-1:]
Related