简体   繁体   中英

two single double quotes in Python

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

Try like this:

words = ['abc', 'acd', 'edf']
words = ['\'\'{}\'\''.format(x) for x in words]
print(words)

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''"]

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM