We often come across small issues that turns out to be big. While coding, the small small tasks become sometimes tedious when not handled well. One among those tasks is output formatting in which we require to omit the quotes while printing any list elements. Let’s discuss certain ways in which this can be performed.
Method #1 : Using join()
We can simplify this task by using the join method in which we join the strings in the list together by the separator being passed ( in this case comma ) and hence solve the issue.
# Python3 code to demonstrate # avoiding printing last comma# using join() # initializing listtest_list = ['Geeks', 'For', 'Geeks'] # printing original list print ("The original list is : " + str(test_list)) # using join()# avoiding printing last commaprint ("The formatted output is : ")print (', '.join(test_list)) |
The original list is : ['Geeks', 'For', 'Geeks'] The formatted output is : Geeks, For, Geeks
Method #2 : Using print() + sep
The print function can be used by passing the required container containing the strings and * operator performs the task of joining each string in this case. The separator being used is defined using the sep keyword passed as second argument in print.
# Python3 code to demonstrate # avoiding printing last comma# using print() + sep # initializing listtest_list = ['Geeks', 'For', 'Geeks'] # printing original list print ("The original list is : " + str(test_list)) # using print() + sep# avoiding printing last commaprint ("The formatted output is : ")print(*test_list, sep =', ') |
The original list is : ['Geeks', 'For', 'Geeks'] The formatted output is : Geeks, For, Geeks
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


