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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 =', ')

chevron_right


Output :

The original list is : ['Geeks', 'For', 'Geeks']
The formatted output is : 
Geeks, For, Geeks


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.