Skip to content
Advertisement

How to remove last comma from print(string, end=“, ”)

my output from a forloop is

JavaScript

I use the below code to have them on the same line

JavaScript

and then I get

JavaScript

I want the final result to be

JavaScript

How do I change the code print(string, end=", ") so that there is no , at the end

The above string is user input generated it can me just 1 or 2,3, 45, 98798, 45 etc

So far I have tried

JavaScript

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

JavaScript

gets

JavaScript

Advertisement

Answer

You could build a list of strings in your for loop and print afterword using join:

JavaScript

alternatively, if your something has a well-defined length (i.e you can len(something)), you can select the string terminator differently in the end case:

JavaScript

UPDATE based on real example code:

Taking this piece of your code:

JavaScript

and following the first suggestion above, I get:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement