Monday, November 18, 2024
Google search engine
HomeLanguagesPython | Avoiding quotes while printing strings

Python | Avoiding quotes while printing strings

We often come across small issues that turn out to be big. While coding, the small tasks become sometimes tedious when not handled well. One of those tasks is output formatting in which we require to omit the quotes while printing any list elements using Python.

Example

Input : ['Geeks', 'For', 'Geeks']
Output : Geeks For Geeks

Avoiding Quotes While Printing Strings in Python

Below are the methods that we will cover in this article:

  • Using join()
  • Using Iteration
  • Using strip()
  • Using reduce()
  • Remove Beginning and Ending Double Quotes

Avoiding Quotes While Printing Strings 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




# initializing list
test_list = ['Geeks', 'For', 'Geeks']
 
# printing original list
print("The given list is : " + str(test_list))
 
# using join() avoiding printing last comma
print("The formatted output is : ")
print(' '.join(test_list))


Output

The given 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 

Print a List Without Quotes in Python 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.

In this we first Initialize an empty string variable, formatted_output after it we Iterate over each element of the input list to 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.

Python3




# 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 : ", 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.

Avoiding Quotes While Printing Strings Using strip()

The print function can be used by passing the required strings and performing the task of joining each string in this case. The separator being used is defined using the sep keyword passed as the second argument in print.

Python3




# using strip()
string = '"Geeks-for-Geeks"'
print(string.strip('"\''))


Output

Geeks-for-Geeks

Remove Quotes From a String Using reduce()

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.

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


Output

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

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.

Remove Beginning and Ending Double Quotes from a String

We can also remove the quotes from a String in Python by using the built-in method of Python replace. With the help of replace if we pass the character which we want to remove in this method then it will remove that character from the string.

Python3




string_with_quotes = '"GFG, is the best!"'
string_without_quotes = string_with_quotes.replace('"', '')
print(string_without_quotes)


Output

GFG, is the best!
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments