Last updated: Apr 9, 2024
Reading timeยท4 min
To remove the quotes from a list of strings:
str.replace()
method to remove the quotes from each string.my_list = ['"bobby"', '"hadz"', '"com"'] # โ Remove all quotes from list of strings new_list = [item.replace('"', '') for item in my_list] print(new_list) # ๐๏ธ ['bobby', 'hadz', 'com'] # ------------------------------------------------- # โ Remove leading and trailing quotes from list of strings new_list = [item.strip('"') for item in my_list] print(new_list) # ๐๏ธ ['bobby', 'hadz', 'com']
We used a list comprehension to remove the quotes from a list of strings.
On each iteration, we remove all occurrences of a double quote by replacing each with an empty string.
The list comprehension returns a new list, in which the strings don't contain quotes.
The examples remove the double quotes from the strings in the list, but you can use the same approach to remove the single quotes from each string.
my_list = ["'bobby'", "'hadz'", "'com'"] new_list = [item.replace("'", '') for item in my_list] print(new_list) # ๐๏ธ ['bobby', 'hadz', 'com']
The example above removes all occurrences of a single quote from each string in the list.
The str.replace() method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.
The method takes the following parameters:
Name | Description |
---|---|
old | The substring we want to replace in the string |
new | The replacement for each occurrence of old |
count | Only the first count occurrences are replaced (optional) |
The method doesn't change the original string. Strings are immutable in Python.
Alternatively, you can use the str.strip()
method.
str.strip()
This is a three-step process:
str.strip()
method to remove the leading and trailing quotes from
each string.my_list = ['"bobby"', '"hadz"', '"com"'] new_list = [item.strip('"') for item in my_list] print(new_list) # ๐๏ธ ['bobby', 'hadz', 'com']
The example uses the str.strip()
method to remove the leading and trailing
quotes from each string in the list.
The str.strip() method takes a string containing characters as a parameter and removes all occurrences of the characters from the front and back of the string.
We passed a double quote to the strip()
method to remove all occurrences of a
double quote at the front and end of each string.
You can also use the str.lstrip()
and str.rstrip()
methods, which remove the
leading or the trailing occurrences of the specified character.
my_list = ['"bobby"', '"hadz"', '"com"'] new_list = [item.lstrip('"') for item in my_list] print(new_list) # ๐๏ธ ['bobby"', 'hadz"', 'com"'] new_list = [item.rstrip('"') for item in my_list] print(new_list) # ๐๏ธ ['"bobby', '"hadz', '"com']
The str.lstrip() method takes a string containing characters as an argument and returns a copy of the string with the specified leading characters removed.
The str.rstrip() method takes a string containing characters as an argument and returns a copy of the string with the specified trailing characters removed.
You can also use a generator expression to remove the quotes from a string.
my_str = "bobby 'hadz' com 'abc' xyz" result = ''.join(char for char in my_str if char != "'") print(result) # ๐๏ธ bobby hadz com abc xyz
We used a generator expression to iterate over the string.
On each iteration, we check if the current character is not equal to a single quote and return the result.
The output is a generator object that contains the characters of the string excluding the single quotes.
The last step is to use the str.join()
method to join the remaining characters
into a string.
my_str = "bobby 'hadz' com 'abc' xyz" result = ''.join(char for char in my_str if char != "'") print(result) # ๐๏ธ bobby hadz com abc xyz
The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
The string the method is called on is used as the separator between the elements.
For our purposes, we called the str.join()
method on an empty string to join
the remaining characters without a separator.
You can learn more about the related topics by checking out the following tutorials: