python - How to remove the last comma in a list of integers? -


the list supposed square of each number. i've managed need remove last comma in sequence.

when use code:

def multiplicator():       in range(3, 20):           b = (a*a)           print(b, end=",")  multiplicator() 

i get:

9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361, 

you can keep loop , add condition:

def multiplicator():     in range(3, 20):         b = (a*a)         print(b, end="")         if a<19: # if not last element           print(end=",") # print ","     print() # print new line after  multiplicator() # => 9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361 

you can use ternary condition shorten code:

def multiplicator():     in range(3, 20):         b = (a*a)         print(b, end="," if a<19 else "")      print() # print new line after multiplicator() # => 9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -