Reputation: 119
I have a list of items, list1
, shown below:
["None", "A ','B", "A"]
The second element of list1
is "A ','B"
.
How can I remove the single quotation mark and get "A, B"
instead?
Upvotes: 2
Views: 14560
Reputation: 626758
To remove a single quote from all strings in a list, use a list comprehension like
l = ["None", "A ','B", "A"]
l = [s.replace("'", '') for s in l]
See the Python demo:
l = ["None", "A ','B", "A"]
l = [s.replace("'", '') for s in l]
print(l) # => ['None', 'A ,B', 'A']
Upvotes: 1
Reputation: 3450
This is a simple string replace.
list1[1] = list1[1].replace("'","")
EDIT: Strings are immutable, so you have to actually do the replace, and reassign. I should add, that by "removing" the single quote, you will not be gaining a space after the comma.
Upvotes: 1
Reputation: 130
list1 = ["None", "A ','B", "A"]
list1[1] = list1[1].replace("'", "")
print(list1)
Is that what you need?
Upvotes: 0