How can I print a single backslash?
You need to escape your backslash by preceding it with, yes, another backslash:
print("\\")
And for versions prior to Python 3:
print "\\"
The \
character is called an escape character, which interprets the character following it differently. For example, n
by itself is simply a letter, but when you precede it with a backslash, it becomes \n
, which is the newline character.
As you can probably guess, \
also needs to be escaped so it doesn't function like an escape character. You have to... escape the escape, essentially.
See the Python 3 documentation for string literals.
How can I print a single backslash?
You need to escape your backslash by preceding it with, yes, another backslash:
print("\\")
And for versions prior to Python 3:
print "\\"
The \
character is called an escape character, which interprets the character following it differently. For example, n
by itself is simply a letter, but when you precede it with a backslash, it becomes \n
, which is the newline character.
As you can probably guess, \
also needs to be escaped so it doesn't function like an escape character. You have to... escape the escape, essentially.
See the Python 3 documentation for string literals.
How to print a single backslash in python in a string?
In IDLE (Python's Integrated Development and Learning Environment), expression values have their representation echoed to stdout.
As https://docs.python.org/3/library/idle.html explains:
The
repr
function is used for interactive echo of expression values.
It returns an altered version of the input string in which control
codes, some BMP codepoints, and all non-BMP codepoints are replaced
with escape codes.
If you want to print a string, use the print()
function. Otherwise you'll get its representation.
Consider the following code entered into IDLE:
>>> hitman_str = "Agent \ 47"
>>> print(hitman_str) # We see only one slash when using print
Agent \ 47
>>> hitman_str # This shows the representation, which shows two slashes
'Agent \\ 47'
>>> print(repr(hitman_str)) # Same as above
'Agent \\ 47'
There are multiple ways to get a string with only one slash:
single_slash1 = "\\"
>>> print(single_slash1)
\
>>> single_slash2 = "\ "[0]
>>> print(single_slash2)
\
>>> single_slash1 == single_slash2
True
Similarly, there are multiple ways to get a string with two consecutive slashes:
>>> two_slashes1 = "\\\\"
>>> print(two_slashes1)
\\
>>> print(repr(two_slashes1))
'\\\\'
>>> two_slashes1
'\\\\'
>>> len(two_slashes1)
2
>>> two_slashes2 = r"\\"
>>> print(two_slashes2)
\\
>>> print(repr(two_slashes2))
'\\\\'
>>> two_slashes2
'\\\\'
>>> len(two_slashes2)
2
We can confirm hitman_str
only has one slash:
>>> hitman_str.count(single_slash1)
1
We can iterate through the string and print each character and its Unicode code point.
As expected, this shows only one slash:
>>> for char in hitman_str:
print(char, ord(char))
A 65
g 103
e 101
n 110
t 116
32
\ 92
32
4 52
7 55
Raw strings are very handy, especially for Windows paths if you don't wanna use os.path
or pathlib
:
>>> filename = r"C:\Users\Lee Hong\Documents\New Letters\Impatient 1999-06-14.txt" # Works fine
>>> filename = "C:\Users\Lee Hong\Documents\New Letters\Impatient 1999-06-14.txt" # Error
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
>>> raw_str = r"This \\\has \11 \\slashes \\and \no \line \break"
>>> print(raw_str)
This \\\has \11 \\slashes \\and \no \line \break
>>> raw_str.count(single_slash1)
11
For more information, including a list of escape sequences to watch out for, refer to https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
Quoting backslashes in Python string literals
You're being mislead by output -- the second approach you're taking actually does what you want, you just aren't believing it. :)
>>> foo = 'baz "\\"'
>>> foo
'baz "\\"'
>>> print(foo)
baz "\"
Incidentally, there's another string form which might be a bit clearer:
>>> print(r'baz "\"')
baz "\"
How to get single '\' (backslash) printed by a function?
When printing a list in python the repr()
method is called in the background. The string stored is '\
', it's just represented as '\\
'.
If you print out every element individually, it will show you the literal value
as opposed to the represented value
:
print(ten(4, 1)[1])
Output:
u^\mu_2
In Python, how do I have a single backslash element in a list?
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.
Related Topics
Python Dictionary Comprehension
How to Call a Script from Another Script
Parse Date String and Change Format
Python Function Global Variables
How to Make One Python File Run Another
Pandas Percentage of Total With Groupby
Why Does Running the Flask Dev Server Run Itself Twice
What Is the Formal Difference Between "Print" and "Return"
How to Pass a String into Subprocess.Popen (Using the Stdin Argument)