Python | Avoiding quotes while printing strings
We often come across small issues that turns out to be big. While coding, the 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
# Python3 code to demonstrate # avoiding printing last comma# using join()# initializing listtest_list = ['Geeks', 'For', 'Geeks']# printing original listprint ("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
# Python3 code to demonstrate # avoiding printing last comma# using print() + sep# initializing listtest_list = ['Geeks', 'For', 'Geeks']# printing original listprint ("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


