1

To start, I'm running Python 3.6 on windows 10.

The backslash character in python is a special character that acts as an "escape" for strings. Because of this, it cannot be printed directly. If I want to print a single backslash, doing this will not work:

print("\") #error message

The way to get around this is to simply add another backslash, like so:

print("\\") #\

So far so good. However, when you make a list of string elements, like below:

list_of_strings = ['<','>','\u20AC','\u00A3','$','\u00A5','\u00A4','\\']
print(list_of_strings)

The following is printed out: ['<', '>', '€', '£', '$', '¥', '¤', '\\'] Which is not ideal. Trying to use just one backslash('\') in the string will result in the same EOL error as mentioned in the beginning. If you try to directly print out the backslash using a single instance of its unicode (u005C), like so:

print(['<','>','\u20AC','\u00A3','$','\u00A5','\u00A4','\u005C'])

The output still has a double slash: ['<', '>', '€', '£', '$', '¥', '¤', '\\']

One other thing I tried was this:

bs = "\\"
print(bs) #prints a single \
list_of_strings = ['a','b','c','d',bs]
print(list_of_strings) #prints ['a', 'b', 'c', 'd', '\\']

So is there a way for me to be able to set this up so that a single backslash will be printed out as a string element in a list?

I appreciate any help you can give me!

  • How about `print(r'\u00A3')`? – AMC Dec 24 '19 at 00:29
  • This should be a duplicate of [Why does printing a tuple in Python double the backslashes?](https://stackoverflow.com/questions/29245234). The problem is the same with a list as with a tuple. – Karl Knechtel Aug 07 '22 at 03:44

4 Answers4

2

Yes, the output will always have a double slash, because that is how string objects represent a single slash. And when you print a list, the list __str__ and __repr__ methods use the objects __repr__ methods to build up what is being printed.

But if you print(list_of_strings[-1]) you will see, it is a single slash. Why does this even matter? The key here is to understand the distinction between source code string literals, the way an object is represented, and what the actual value of the string object is.

If it really bothers you, write a function to print the list yourself:

>>> list_of_strings = ['<','>','\u20AC','\u00A3','$','\u00A5','\u00A4','\\']
>>> def print_list(l):
...     innards = ','.join(l)
...     print(f'[{innards}]')
...
>>> print_list(list_of_strings)
[<,>,€,£,$,¥,¤,\]
>>>

Printing a list object directly should mainly be used for debugging. Whenever you need some particular output formatting, you should be handling that yourself.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
2

When printing a list in python the repr() method is called in the background. This means that print(list_of_strings) is essentially the same thing (besides the newlines) as:

>>> for element in list_of_strings:
...     print(element.__repr__())
... 
'<'
'>'
'€'
'£'
'$'
'¥'
'¤'
'\\'

In actuality the string stored is '\' it's just represented as '\\'

>>> for element in list_of_strings:
...     print(element)
... 
<
>
€
£
$
¥
¤
\

If you print out every element individually as above it will show you the literal value as opposed to the represented value.

Eric S
  • 1,001
  • 10
  • 14
0

Double backslashes is correct way to go. It may seem like it is printing out 2 backslashes is so you can copy/paste it into another script. If you type in print(list_of_strings[-1] you will see only 1 backslash. There is actually no problem here.

MehmedB
  • 1,059
  • 1
  • 16
  • 42
0

For some reason, in the list the back slash '\' appears as double:

list_elements = []
string1 = "\\ string 1"
list_elements.append(string1)
print(list_elements)

>> ['\\ string 1']

But when you access the element of the list, it comes with one backslash:

print(list_elements[0])

>> \ string 1

So, if you need to use each element, you won´t have any problem.