0

I have an empty list and I want to append to it a raw string. For example, I have list1 = [] and I want to append the following string '\"quote\"', so I would get ['\"quote\"']. However when I do the following

list1 = []  
text = '\"quote\"'  
list1.append(text)  
list1  

I get ['"quote"']

when I do the following

list2 = []  
text = r'\"quote\"'  
list2.append(text)  
list2  

I get ['\\"quote\\"']

and when I do the following

list3 = []  
text = '\"quote\"'.replace('\"','\\"')  
list3.append(text)  
list3  

I get ['\\"quote\\"']

How can I get ['\"quote\"']?

I use Jupyter and Python 3.5.2.

Thanks!

Don
  • 19
  • 1
  • 2
  • You don't, because Python will never give that representation. – Ignacio Vazquez-Abrams Mar 21 '18 at 02:03
  • 1
    python is escaping the characters for you when printing the list. If you `print` the individual string value it should show up as you want – avigil Mar 21 '18 at 02:04
  • 1
    `'\\"quote\\"'` is just the string representation of the string `\"quote\"`. You get that when you try to convert the list as a whole to a string. – chepner Mar 21 '18 at 02:04
  • 5
    Don't just output the variable as the last line of a Jupyter cell... Actually use print function – OneCricketeer Mar 21 '18 at 02:06
  • It's not clear if you want `'"quote"'` or `r"\"quote\""` – OneCricketeer Mar 21 '18 at 02:07
  • @cricket_007: not sure what you mean. It's a list, so printing will give the same output. What do you mean? – Don Mar 21 '18 at 02:21
  • 1
    How do you want to use this string, and what sequence of characters do you want it to contain? It's clear that you're confused about the relationship between the string literal you write, how the string displays, and what's actually in the string. We need more information to know how to explain things and determine what the final code should look like. – user2357112 supports Monica Mar 21 '18 at 02:27
  • 1
    There's a difference between `repr(data)` and `print(data)`. I might suggest playing around with them – OneCricketeer Mar 21 '18 at 02:27
  • @cricket_007: repr(data) indeed changes the output but doesn't give the desired one. For example, repr(list1) gives '[\'"quote"\']' and not ['\"quote\"']. – Don Mar 21 '18 at 02:32
  • @user2357112: I want to have a list of raw strings. I want each element in the list will contain the exact same characters of the relevant string. Is it clear? – Don Mar 21 '18 at 02:35
  • I meant `print(repr(data))` vs `print(data)`, by the way. And yes, the representation of the list will include the *string representation of the object*, which is a list with an *escaped string* – OneCricketeer Mar 21 '18 at 02:35
  • 2
    What **you see** as output is not what Python **stores**. That's what we're all trying to tell you – OneCricketeer Mar 21 '18 at 02:37
  • @Don: That's not how Python works. Raw strings are not a distinct kind of string; raw string *literals* are a different syntax for writing ordinary strings. Also, "I want each element in the list will contain the exact same characters of the relevant string." doesn't tell us what you want the characters in the string to be, and you still haven't told us what you want to do with these strings. – user2357112 supports Monica Mar 21 '18 at 02:39
  • @cricket_007: I understand, but what I care now is what I see and not what is stored. – Don Mar 21 '18 at 02:44

2 Answers2

0

Let's clarify something. The fact you have a list is not important, and these are the same

>>> '"quote"' == "\"quote\""
True
>>> '"quote"' == '\"quote\"'
True

Now, let's use a raw string.

>>> '\"quote\"' == r'\"quote\"'
False

As expected, so we print the raw string, also expected.

>>> print(r'\"quote\"')
\"quote\"

But how is that raw string represented?

>>> repr(r'\"quote\"')
=> '\'\\\\"quote\\\\"\''

Weird, right? And how about we print the representation?

>>> print(repr(r'\"quote\"'))
'\\"quote\\"'

That's better, but can you remove the \\" with just \"? No - you cannot. \" is an escape character, and the backslash itself needs escaped, and that is what you are seeing in the output.


To answer the question, list2 is stored correctly, you are just looking at the output incorrectly.

See

>>> list2
['\\"quote\\"']
>>> print(list2[0])
\"quote\"
>>> list2[0]
'\\"quote\\"'
OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
0

Short Answer

if you're asking 'how to get it to work'.. then an easy solution would be to write a function that simply converts the list to a string.

if you run

print('\'' + r'\"quote\"' + '\'')

You will see

'\"quote\"'

Like you wanted. Now just need to apply this logic on a list. So it will be something like (I am using python 2.7.14 but I assume it is similar to your version):

def prettyPrintList(l):
    print('[' + ','.join(map(lambda x: '\'' + x + '\'', l)) + ']')


prettyPrintList([r'\"quote\"'])

There's probably a nicer way to write it in python, I don't usually use python.

Long Answer

I would like to address the origin of the problem: "Why doesn't it just work?"

In OO languages - like python - when you print an object, like a list, it actually calls a function implicitly.

In python that function is str().

So technically you are running the code

print list.__str()__

That function defines the behavior that you see.
For lists, I assume it does something very similar to my "prettyPrint" function - It probably iterates over all the elements and calls .__str__() on them, then joins them with a comma and wraps with brackets.

So in order to understand the output, lets have a look at .__str__() when we call it directly on '\"quote\"'

>>> '\"quote\"'
'"quote"'
>>> '\"quote\"'.__str__()
'"quote"'
>>> r'\"quote\"'.__str__()
'\\"quote\\"'
>>> 

As you can see, the REPL actually outputs the result of .__str__(). So again, the result will be depended on that function's definition.

It seems to me that python chose a weird way to implement it.

If I do the same in JavaScript it yields different results between the browser and node (in console). In the browser it yields a similar result to what you desire

console.log(['\\"item\\"'])
 > ["\"item\""]

And in the console

> console.log(['\\"quote\\"'])
[ '\\"quote\\"' ]
undefined
> console.log(['\"quote\"'])
[ '"quote"' ]

So as you can see it really depends on the implementation of the core function.

The best way you can guarantee things to be printed exactly like you need it, is to specify in code how you want to "stringify" it. basically, implement the function .__str__() the way that suites you.

I hope I answered your question and convinced you about the solution.
If you have any more comments, please let me know

guy mograbi
  • 22,955
  • 13
  • 75
  • 115