0

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

brodo80
  • 83
  • 3
  • `'CWA9','WQH0','GBD0'` are 3 values. What do you mean by store it as *a* variable? – BTables Oct 05 '21 at 15:22
  • 1
    That's how Python represents the string, so you know it's a string. It's not part of the string, it's part of the representation of the string. – Peter Wood Oct 05 '21 at 15:22
  • 1
    You *already have* what are you trying to get – 2e0byo Oct 05 '21 at 15:23
  • 1. variable meaning stored in memory so it can be passed through further in program. Var would be 'CWA9','WQH0','GBD0'. 2. Yes, " " is string representation, how do I store without " "? I need it stored without " " – brodo80 Oct 05 '21 at 15:37
  • It is not stored with "". The "" is just shown when you do print! Look at my answer below. I print the first character and it is the single quote, not the double quote. – Lidia Parrilla Oct 05 '21 at 15:40

2 Answers2

1

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'
Jonatan Öström
  • 2,428
  • 1
  • 16
  • 27
  • 1
    If this is really the only objective, then this is clearly a duplicate question of many others, like https://stackoverflow.com/questions/12453580/how-to-concatenate-items-in-a-list-to-a-single-string or https://stackoverflow.com/questions/5618878/how-to-convert-list-to-string – Lidia Parrilla Oct 05 '21 at 15:28
  • Hi there Jonatan. Thanks for the response. My apologies, I should have been more specific there. I would need it to be with quotes so that it would read 'CWA9','WQH0','GBD0'. I need so I can pass it through the in clause in SQL so that – brodo80 Oct 05 '21 at 15:31
  • select * from table where var in ( '' , '' , ''). – brodo80 Oct 05 '21 at 15:32
  • 2
    @brodo80 put what you're trying to achieve in the question. If you're using sqlite you should let it populate the SQL for you. – Peter Wood Oct 05 '21 at 15:36
  • this is an x/y problem – 2e0byo Oct 05 '21 at 16:01
1

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.

Lidia Parrilla
  • 549
  • 3
  • 13