-2

I have a list of strings but I want to make single double quotes before and after each string. If I have the list

words = ['abc', 'acd', 'edf']

I want to change it to:

words = [''abc'', ''cdf'', ''edf'']

How can I do it with list comprehension or other method? I want two sets of single quotation marks. I want this to pass to redshift to unload the query result to s3. https://docs.aws.amazon.com/redshift/latest/dg/r_UNLOAD.html

3
  • 1
    That is not something you can do. Because double quotes and single quotes doesn't have difference, at least in terms of this question. Anyways, why do you want to do that?
    – Tugay
    Oct 29, 2020 at 0:46
  • I want to pass it to redshift unload function docs.aws.amazon.com/redshift/latest/dg/r_UNLOAD.html Oct 29, 2020 at 0:48
  • "If your query contains quotation marks (for example to enclose literal values), put the literal between two sets of single quotation marks" Oct 29, 2020 at 0:51

3 Answers 3

1

Try like this:

words = ['abc', 'acd', 'edf']
words = ['\'\'{}\'\''.format(x) for x in words]
print(words)
1
  • Edited please check now
    – Wasif
    Oct 29, 2020 at 0:52
0

well python syntax won't allow

words = [''abc'', ''cdf'', ''edf'']

we have to use escape characters \" or \'; in your case since you want two: \"\" or \'\'

words = ['abc', 'cdf', 'edf']
words = [f"\'\'{word}\'\'" for word in words]

now the following is stored in words:

["''abc''", "''cdf''", "''edf''"]

you can also use f strings wrapped with double quotations and normally add single quotations (im not saying this is proper but it does work)

words = [f"''{word}''" for word in words]
["''abc''", "''cdf''", "''edf''"]
-1

Maybe I am misunderstanding you but cant you just use double quotes to signify the string notation and use the single quotes for the string content?, then just concatenate with +:

words = ['abc', 'acd', 'edf']
new_list = ["''"+item+"''" for item in words]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.