S.J
S.J

Reputation: 119

Python: remove single quotation in a string in a list

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

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

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

Joshua Schlichting
Joshua Schlichting

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

Luis
Luis

Reputation: 130

list1 = ["None", "A ','B", "A"]
list1[1] = list1[1].replace("'", "")
print(list1)

Is that what you need?

Upvotes: 0

Related Questions