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

Related Articles

Python | Avoiding quotes while printing strings

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like 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

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

  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.

Method #4: Using iteration
We can also use a loop to iterate over the list and concatenate the elements with a comma separator. This method is straightforward and easy to understand.

Algorithm:

Initialize an empty string variable, formatted_output.
Iterate over each element of the input list.
Append the current element to the formatted_output variable.
If the current element is not the last element, append a comma and space to the formatted_output variable.
Print the original list and the formatted output.

Python3




# Python code to demonstrate
# avoiding printing last comma
# using a loop
  
# initializing list
test_list = ['Geeks', 'For', 'Geeks']
  
# printing original list
print("The original list is : " + str(test_list))
  
# using a loop
# avoiding printing last comma
formatted_output = ""
for i, element in enumerate(test_list):
    formatted_output += element
    if i != len(test_list) - 1:
        formatted_output += ", "
  
print("The formatted output is : ")
print(formatted_output)

Output

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

Time complexity:
The time complexity of this code is O(n), where n is the length of the input list, because we are iterating over each element of the list once.

Auxiliary Space:
The space complexity of this code is also O(n), because the formatted_output variable takes up space proportional to the size of the input list.


Last Updated : 02 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials