The Wayback Machine - https://web.archive.org/web/20230326130207/https://www.geeksforgeeks.org/python-avoiding-quotes-while-printing-strings/
Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Avoiding quotes while printing strings

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 02 Jan, 2023
Improve Article
Save Article

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

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. 

Python3




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

Output : 

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

Method #3 : Using reduce()

Here is an example of how you can use the reduce function from the functools module to format the output of a list in Python, while avoiding quotes:

Python3




from functools import reduce
 
# Initialize the test list
test_list = ['Geeks', 'For', 'Geeks']
 
# Use reduce to concatenate the list elements with a comma separator
output = reduce(lambda x, y: f"{x}, {y}" if x != "" else y, test_list)
 
# Print the original list and the formatted output
print(f"The original list is: {test_list}")
print(f"The formatted output is: {output}")
#This code is contributed by Edula Vinay Kumar Reddy

Output

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

The reduce function applies a given function to the elements of the list, starting from the left and cumulatively reducing the list to a single value. In this case, the function concatenates the current element with the previously reduced value, using a comma separator if the reduced value is not an empty string.

This code has a time complexity of O(n) where n is the length of the list, because the reduce function iterates over each element in the list once. The space complexity is also O(n), because the output variable takes up space proportional to the size of the input list.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!