2
s = "ez , dad , tada"
print(s.split(" , "))

prints the following:

['ez', 'dad', 'tada']

The problems is, I need the output to be with double quotes, not with single quotes, like this:

["ez", "dad", "tada"]
TheEagle
  • 5,808
  • 3
  • 11
  • 39
Beast Who
  • 45
  • 7
  • 6
    That's just how Python represents lists of strings, when you print them. There's no functional difference between `'` and `"`. And that comma at the end doesn't really make sense in this context. – CrazyChucky Apr 25 '21 at 20:15
  • 1
    So, to be clear, you want the actual `str` representation to include the double quotes? Like this: `'"ez"'`? What about `[f'"{e}"' for e in s.split(" , ")]`? – gmdev Apr 25 '21 at 20:15
  • @CrazyChucky they need it in this format the line could be added new here and I don't know how to add it at all – Beast Who Apr 25 '21 at 20:16
  • 1
    ...Do you want a list, or do you want a string representing the list? – CrazyChucky Apr 25 '21 at 20:16
  • @CrazyChucky string representing the list? – Beast Who Apr 25 '21 at 20:17
  • @gmdev give me complete code thanx – Beast Who Apr 25 '21 at 20:19
  • @Xiddoc ( s = "ez , dad , tada" print(s.split(" , ")) give mi ['ez', 'dad', 'tada'] ( im search ["ez", "dad", "tada"], ) this is ok – Beast Who Apr 25 '21 at 20:23
  • 1
    If you don't want Pythons representation of a list you have to write your own code. – Matthias Apr 25 '21 at 20:25
  • 1
    @BeastWho check out the answers on this question (this is the comment section, below this are the answers), and if you find any useful ones make sure to upvote them / mark them as valid. If not, then comment on them to see how they can improve their answers. – Xiddoc Apr 25 '21 at 20:26
  • 1
    Does this answer your question? [Return a variable in a Python list with double quotes instead of single](https://stackoverflow.com/questions/32606599/return-a-variable-in-a-python-list-with-double-quotes-instead-of-single) – Jacques Gaudin Apr 25 '21 at 20:54

2 Answers2

3

Credit to @fn. for this posting

Example:

import json

s = "ez , dad , tada"

print(json.dumps(s.split(" , ")))

Output:

["ez", "dad", "tada"]
rhurwitz
  • 2,557
  • 2
  • 10
  • 18
  • 1
    Good answer but next time around you can flag the question as a duplicate of the linked post. It helps keeping the site tidy. – Jacques Gaudin Apr 26 '21 at 09:27
1

You've shown that you used the following code to split up a string into an list:

s = "ez , dad , tada"

split_data = s.split(" , ")

Now that you have the data in an list, you can reformat it by using the .join() method of a string, to turn the list into a string. According to your example, you want to format the string like ["ez", "dad", "tada"], so what you can do is to merge each 2 items with ", " since that seems to be the seperating string.

separator = '", "'

print('["' + separator.join(split_data) + '"]')
Xiddoc
  • 3,369
  • 3
  • 11
  • 37
  • 2
    Tip: these are lists. Python *does* have arrays, but [they're different](https://stackoverflow.com/questions/176011/python-list-vs-array-when-to-use). – CrazyChucky Apr 25 '21 at 20:23
  • You're absolutely correct. I keep mixing them up since I've never actually used an array in Python... – Xiddoc Apr 25 '21 at 20:24