Python:: how to put double quotes for values in a list

16,320
  • strip the []: abc[1:-1]
  • split on ,: .split(',')
  • bracket each element with "": '"'+x+'"'
  • join with ,: ','.join
  • add the []: '['+...+']'

TL;DR:

'['+','.join(['"'+x+'"' for x in abc[1:-1].split(',')])+']'
Share:
16,320

Related videos on Youtube

bruce_karlo
Author by

bruce_karlo

Updated on September 18, 2022

Comments

  • bruce_karlo
    bruce_karlo almost 2 years

    In Python Scripting, I have a string abc=[9874,209384,20938]

    I want the output as abc=["9874","209384","20938"]

    My efforts got me "9874" "209384" "20938"

    but I want like this ["9874","209384","20938"]

    • Anaksunaman
      Anaksunaman about 5 years
      abc=['"9874"','"209384"','"20938"'] perhaps? Note that these examples are wrapped in single ' ' quotes (outside) then double " " quotes (inside).