Python | Avoiding quotes while printing strings
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 list test_list = ['Geeks', 'For', 'Geeks'] # printing original list print ("The original list is : " + str(test_list)) # using join() # avoiding printing last comma print ("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 list test_list = ['Geeks', 'For', 'Geeks'] # printing original list print ("The original list is : " + str(test_list)) # using print() + sep # avoiding printing last comma print ("The formatted output is : ") print(*test_list, sep =', ') |
The original list is : ['Geeks', 'For', 'Geeks'] The formatted output is : Geeks, For, Geeks
Recommended Posts:
- Python dictionary (Avoiding Mistakes)
- Python | Avoiding class data shared among the instances
- Single and Double Quotes | Python
- Python | Ways to print list without quotes
- Python | Printing list vertically
- Multi-Line printing in Python
- Programs for printing pyramid patterns in Python
- Python | Remove empty strings from list of strings
- Python | Tokenizing strings in list of strings
- Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing)
- Python | Interleaving two strings
- C strings conversion to Python
- Python Strings decode() method
- Python Strings encode() method
- Python | C Strings of Doubtful Encoding | Set-2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



