Assume I am creating a checkout system for a store. This is a list of the products:
products = ["Apple", "Banana", "Cherry", "Durian"]
And I want to join them with commas. So, I can use this:
", ".join(products) # Returns "Apple, Banana, Cherry, Durian"
What if I want to limit the length of the output string? Say, I want to output "Apple, Banana…" if the final string is longer than 20 characters?
The problem is, I want to prevent the names from being cut, so I don't want "Apple, Banana, Cher…". In some cases, the strings in products may contain commas, so I don't want to split the string with commas.