-2

How to print out a list of empty strings of size n while the empty strings are represented by double quotes "" not single quotes '' (["", "", ""]) in python2?

I want to initialize a list of ["", "", "", ""] not a list of ['', '', '', ''] of a given size n, it has nothing to do with JSON format.

And why does python automatically convert all the double quotes into single quotes?
i.e. print ["abc"] will print out ['abc'] and print [""] will print out ['']

So far I can only get single quotes printed out [''] but the output I'm seeking strictly requires double quotes [""] inside and how to do that?

print [""] will print out ['']

print [`""`] will print out ["''"]

print ['""'] will print out ['""']

Gary Bao 鲍昱彤
  • 2,608
  • 20
  • 31
  • 3
    Why not just `print '[""]'`? – melpomene Jul 25 '19 at 15:00
  • Is the output required to be JSON? Then `print(json.dumps(a))`. – Charles Duffy Jul 25 '19 at 15:01
  • To be clear, the type of quotes you use specifying a literal aren't part of the value of that literal; they don't get stored or represented anywhere in memory after the parsing is done. Thus, Python *can't* preserve them (and anyhow, it would make no sense if it did -- `"foo" != 'foo'` would be wildly confusing as a result). – Charles Duffy Jul 25 '19 at 15:03
  • `a=[""]` and then `json.dumps(a)` prints '[""]'. Is this fine? – fiveelements Jul 25 '19 at 15:03
  • @fiveelements, ...didn't I say that in an earlier comment? – Charles Duffy Jul 25 '19 at 15:05
  • If you want to print out `["", "", ""]`, use `print '["", "", ""]'`. – melpomene Jul 25 '19 at 15:09
  • you are totally missing my point, and maybe I wasn't clear, I've never posted a question here before. I need a list of empty strings represented by double quotes, not single quotes. I want to initialize a list of size n filled with empty strings as a place holder. – Gary Bao 鲍昱彤 Jul 25 '19 at 15:18
  • 1
    "*a list of `["", "", "", ""]` not a list of `['', '', '', '']`*" - Those are the same thing in Python syntax. If you don't want Python syntax, don't pass a list to `print`; pass a string. (Also, what syntax do you want exactly?) – melpomene Jul 25 '19 at 15:19
  • @BaoYutong鲍昱彤, ...*why* does the type of quotes matter? If it matters because the consumer of this output is using a JSON parser to read it (which is the most common situation where we see this question asked), then you need to use a JSON library to generate it as output. By contrast, if the consumer is using some other language parser, the place to start is figuring out exactly what that parser is, whether the language it accepts is a superset of anything there's a prebuilt Python generation library for, etc. – Charles Duffy Jul 25 '19 at 15:29
  • @BaoYutong鲍昱彤, ...regardless, it would do a lot of good -- if you can't accept the existing advice you were given to encode the data as JSON -- to go into some detail about exactly which format is needed, and how/why that format isn't in fact a superset of JSON. – Charles Duffy Jul 25 '19 at 15:30

1 Answers1

2

How you represent your data in code is not part of that data itself. That is, "foo" and 'foo' and r'foo' are all the exact same string. There's no semantic difference between them (after parsing, the way they're stored in memory is identical), so they get printed the same way.

If you want to print something in JSON syntax (and double quotes are the only type valid for strings in JSON), use json.dumps() to generate a JSON encoding of your list (and its enclosed string). This will necessarily use double quotes, because if it didn't use double quotes, the result would not be valid JSON.

That is:

import json

a = ['']
print(json.dumps(a))

...will have your intended result.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • this is actually good answer. I'm working on a restricted online IDE and I didn't know json was an internal package that comes with stock python and doesn't need to be installed additionally. A lot people were doubting the legitimacy of my question, but I think it was worth it. – Gary Bao 鲍昱彤 Jul 25 '19 at 15:41
  • The thing to take away from this is that single quotes and double quotes are literally the same https://stackoverflow.com/a/144163/7768490 and so is ``['']`` and ``[""]``. It's just for some reason ``[""]`` will print out ``['']`` and the only way to print out ``[""]`` is using ``json.dumps([""])`` – Gary Bao 鲍昱彤 Jul 25 '19 at 15:43
  • 1
    "for some reason" -- because the in-memory representation doesn't track which quotes were used (and some data was read from a file, a network, or somewhere else where there weren't quotes around it in the first place), Python has to pick *something* to use by default in its `repr()` implementation. It happens to use single quotes, unless the data being printed includes single quotes (and no double quotes), in which case it happens to use double quotes instead... but that's an implementation detail; only documented guarantees, and not implementation details, should be relied on in your code. – Charles Duffy Jul 25 '19 at 15:47