简体   繁体   中英

Remove beginning and ending " from a string and store as variable

I have a simple python list below called lis

    lis = ['CWA9','WQH0','GBD0']

When I print and slice the list, I get the format I desire, but cannot figure how to store as a variable

    print (str(lis)[1:-1])

    'CWA9','WQH0','GBD0'

I tried the following, but I get the beginning and ending " marks (string format).

    str(lis)[1:-1]

    "'CWA9','WQH0','GBD0'"

Whats the best approach to format the original list (lis) to the format seen in the print code above so that the output is 'CWA9','WQH0','GBD0' and store as a variable

Updated: You can do

lis = ['CWA9','WQH0','GBD0']
lis = [f"'{x}'" for x in lis] #New line in update
lis = ', '.join(lis)

then print(lis) gives

'CWA9', 'WQH0', 'GBD0'

The double quotes are not part of the output. You can check it by:

lis = ['CWA9','WQH0','GBD0']

a = str(lis)[1:-1]

a[0]

This will print:

"'"

Which means your first character is actually the single quote. Double quotes are only printed to let you know it is a string.

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