Home > Blockchain >  Remove beginning and ending " from a string and store as variable
Remove beginning and ending " from a string and store as variable

Time:10-06

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

CodePudding user response:

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.

CodePudding user response:

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'
  • Related