In Python, there are several methods for removing quotes from a string. You may need to remove all quotes or only those that surround a string. You may need to remove single or double quotes as well.
If we print a given list of strings as is, we must use quotes and properly fill in a pair of matching quotes. We can avoid using quotes in print statements.
In this article let us look at how to remove these quotes on strings using the below-given methods:
- Using join() Function
- Using the print() & sep keyword
Avoiding quotes while printing strings in Python
- Using join() function (Static Input)
- Using join() Function (User Input)
- Using the print() & sep keyword (Static Input)
- Using the print() & sep keyword (User Input)
Method #1: Using join() function (Static Input)
Join(sequence) joins elements and returns the combined string. Every element of the sequence is combined using the join methods.
A sequence is passed as an argument to the join method. The sequence is written as a single argument: you must surround it with brackets.
You can pass a variable containing the sequence as an argument if you want. This improves readability.
The join() method joins all items in an iterable into a single string.
As the separator, a string must be specified.
Syntax:
string.join(iterable)
Parameters:
iterable: This is required. Any iterable object whose returned values are all strings.
Approach:
- Give the list of strings as static input and store it in a variable.
- Print the given list
- Remove the quotes for the strings in a given list using the join() function by passing the given list as an argument to it by specifying the separator as comma(,) delimiter.
- The Exit of the Program.
Below is the implementation:
# Give the list of strings as static input and store it in a variable. gvn_lst = ['Hello', 'this', 'is', 'PythonPrograms'] # Print the given list print ("The given list = ", gvn_lst) # Remove the quotes for the strings in a given list using the join() function # by passing the given list as an argument to it. print ("The given list after removing quotes on strings: ") print (', '.join(gvn_lst))
Output:
The given list = ['Hello', 'this', 'is', 'PythonPrograms'] The given list after removing quotes on strings: Hello, this, is, PythonPrograms
Method #2: Using join() Function (User Input)
Approach:
- Give the list of strings as user input using the list(),map(),split(),str functions and store it in a variable.
- Print the given list.
- Remove the quotes for the strings in a given list using the join() function by passing the given list as an argument to it by specifying the separator as comma(,) delimiter.
- The Exit of the Program.
Below is the implementation:
# Give the list of strings as user input using the list(),map(),split(),str # functions and store it in a variable. gvn_lst = list(map(str, input( 'Enter some random List Elements separated by spaces = ').split())) # Print the given list print ("The given list = ", gvn_lst) # Remove the quotes for the strings in a given list using the join() function # by passing the given list as an argument to it. print ("The given list after removing quotes on strings: ") print (', '.join(gvn_lst))
Output:
Enter some random List Elements separated by spaces = Welcome to pythonprograms The given list = ['Welcome', 'to', 'pythonprograms'] The given list after removing quotes on strings: Welcome, to, pythonprograms
Method #3: Using the print() & sep keyword (Static Input)
Approach:
- Give the list of strings as static input and store it in a variable.
- Print the given list
- Remove the quotes for the strings in a given list by passing the separator as comma(,) as delimiter and print the result.
- The Exit of the Program.
Below is the implementation:
# Give the list of strings as static input and store it in a variable. gvn_lst = ['Hello', 'this', 'is', 'PythonPrograms'] # Print the given list print ("The given list = ", gvn_lst) # Remove the quotes for the strings in a given list by passing the # separator as comma(,) as delimiter and print the result print ("The given list after removing quotes on strings: ") print(*gvn_lst, sep =', ')
Output:
The given list = ['Hello', 'this', 'is', 'PythonPrograms'] The given list after removing quotes on strings: Hello, this, is, PythonPrograms
Method #4: Using the print() & sep keyword (User Input)
Approach:
- Give the list of strings as user input using the list(),map(),split(),str functions and store it in a variable.
- Print the given list.
- Remove the quotes for the strings in a given list by passing the separator as comma(,) as delimiter and print the result.
- The Exit of the Program.
Below is the implementation:
# Give the list of strings as user input using the list(),map(),split(),str # functions and store it in a variable. gvn_lst = list(map(str, input( 'Enter some random List Elements separated by spaces = ').split())) # Print the given list print ("The given list = ", gvn_lst) # Remove the quotes for the strings in a given list by passing the # separator as comma(,) as delimiter and print the result print ("The given list after removing quotes on strings: ") print(*gvn_lst, sep =', ')
Output:
Enter some random List Elements separated by spaces = Welcome to pythonprograms The given list = ['Welcome', 'to', 'pythonprograms'] The given list after removing quotes on strings: Welcome, to, pythonprograms