Open In App

Python | Avoiding quotes while printing strings

Last Updated : 20 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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!


Similar Reads

Python | Printing String with double quotes
Many times, while working with Python strings, we have a problem in which we need to use double quotes in a string and then wish to print it. This kind of problem occurs in many domains like day-day programming and web-development domain. Lets discuss certain ways in which this task can be performed. Method #1 : Using backslash ("\") This is one wa
4 min read
Python dictionary (Avoiding Mistakes)
What is dict in python ? Python dictionary is similar to hash table in languages like C++. Dictionary are used to create a key value pair in python. In place of key there can be used String Number and Tuple etc. In place of values there can be anything. Python Dictionary is represented by curly braces. An empty dictionary is represented by {}. In P
4 min read
Python | Avoiding class data shared among the instances
Class attributes belong to the class itself and they will be shared by all the instances and hence contains same value of each instance. Such attributes are defined in the class body parts usually at the top, for legibility. Suppose we have the following code snippet : C/C++ Code # Python code to demonstrate # the working of the sharing # of data v
2 min read
Avoiding elif and ELSE IF Ladder and Stairs Problem
This article focuses on discussing the elif and else if ladder and stairs problem and the solution for the same in the C and Python Programming languages. The ELIF and ELSE IF Ladder and Stairs ProblemThere are programmers who shy away from long sequences of IF, ELSE IF, ELSE IF, ELSE IF , etc. typically ending with a final ELSE clause. In language
6 min read
Python | Ways to print list without quotes
Whenever we print list in Python, we generally use str(list) because of which we have single quotes in the output list. Suppose if the problem requires to print solution without quotes. Let's see some ways to print list without quotes. Method #1: Using map() C/C++ Code # Python code to demonstrate # printing list in a proper way # Initialising list
3 min read
Single and Double Quotes | Python
Python string functions are very popular. There are two ways to represent strings in python. String is enclosed either with single quotes or double quotes. Both the ways (single or double quotes) are correct depending upon the requirement. Sometimes we have to use quotes (single or double quotes) together in the same string, in such cases, we use s
3 min read
Triple Quotes in Python
Spanning strings over multiple lines can be done using python's triple quotes. It can also be used for long comments in code. Special characters like TABs, verbatim or NEWLINEs can also be used within the triple quotes. As the name suggests its syntax consists of three consecutive single or double-quotes. Syntax: """ string""" or ''' string''' Note
2 min read
Python - Remove double quotes from dictionary keys
Given dictionary with string keys, remove double quotes from it. Input : test_dict = {'"Geeks"' : 3, '"g"eeks' : 9} Output : {'Geeks': 3, 'geeks': 9} Explanation : Double quotes removed from keys. Input : test_dict = {'"Geeks"' : 3} Output : {'Geeks': 3} Explanation : Double quotes removed from keys. Method #1 : Using dictionary comprehension + rep
6 min read
How to Escape Quotes From String in Python
The single or double quotation is a unique character that can be used in a Python program to represent a string by encapsulating it in matching quotes. When the quotes are used inside a string contained in similar quotes it confuses the compiler and as a result, an error is thrown. This article will demonstrate how to escape quotes from a string in
2 min read
Single Vs Double Quotes in Python Json
JSON (JavaScript Object Notation) is a lightweight data-interchange format widely used for data storage and exchange between web servers and clients. In Python, dealing with JSON is a common task, and one of the decisions developers face is whether to use single or double quotes for string representation within JSON objects. In this article, we'll
3 min read
Practice Tags :
three90RightbarBannerImg