How to write \n without making a newline [duplicate] - python

This question already has answers here:
How can I put an actual backslash in a string literal (not use it for an escape sequence)?
(4 answers)
Closed 3 years ago.
So I'm trying to write this exact string but I don't \n to make a new line I want to actually print \n on the screen. Any thoughts on how to go about this? (using python
Languages:\npython\nc\njava

adding a backslash will interpret the succeeding backslash character literally. print("\\n").

Either escape the backslash by preceding it with another backslash:
'Languages:\\npython\\nc\\njava'
Or use a raw string by preceding the literal with an r:
r'Languages:\npython\nc\njava'

Related

How to use escape characters in Python? [duplicate]

This question already has answers here:
How to escape “\” characters in python
(4 answers)
Closed 4 years ago.
I am totally confused with the escape characters in Python. Sometimes I expect it to output one single '/', it prints '//'; Sometimes I use '//' as '/' it works, but other times it doesn't; And so on so on...
Some example:
print('\\hello') #output --> \hello
print(['\\hello']) #output --> ['\\hello']
So how should I understand '\hello', as '\hello' or '\\hello'? Can anyone explain the mechanism of escape characters more generally?
Firstly there is the question of getting the right characters into your strings. Then there is the question of how Python displays your string. The same string can be displayed in two different ways.
>>> s = '\\asd'
>>> s
'\\asd'
>>> print(s)
\asd
In this example the string only has one slash. We use two slashes to create it but that results in a string with one slash. We can see that there's only one slash when we print the string.
But when we display the string simply by typing s we see two slashes. Why is that? In that situation the interpreter shows the repr of the string. That is it shows us the code that would be needed to make the string - we need to use quotes and also two slashes on our code to make a string that then has one slash (as s does).
When you print a list with a string as an element we will see the repr of the string inside the list:
>>> print([s])
['\\asd']

how to create a raw string when it's last character is a `\` in python [duplicate]

This question already has answers here:
Why can't Python's raw string literals end with a single backslash?
(13 answers)
Closed 5 years ago.
As we all know,we can create a string like this:
str1 = r"\abc\test"
But if I want put the \ in the end of a string like:
str2 = r"\abc\test\"
A syntax error occurs !
I have found an answer,but it's in JavaScript.
`String.raw` when last character is `\`
So,How to deal this in python
You can concat another normal string:
>>> r'\abc\test' + '\\'
'\\abc\\test\\'
duplicated Why can't Python's raw string literals end with a single backslash?
https://docs.python.org/3/reference/lexical_analysis.html
Specifically, a raw literal cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the literal, not as a line continuation.

Bypass '\' in python [duplicate]

This question already has answers here:
What exactly do "u" and "r" string prefixes do, and what are raw string literals?
(7 answers)
How can I put an actual backslash in a string literal (not use it for an escape sequence)?
(4 answers)
Closed 6 years ago.
What is the equivalent of
string c# = #"\hello"
in python?
python = '\hello'
In other words, how would I bypass the '\'' breakpoint in python?
Assuming you mean that you want \ to not act like an escape character, then you simply escape \ with another \:
myString = "\\hello"
print myString
This prints:
\hello
You also can use a "raw" string:
myString = r"\hello"
"I am 6'2\" tall." # escape double-quote inside string
'I am 6\'2"
tall.' # escape single-quote inside string
A general rule of thumb in any programming language to print the escape character or use it as a part of string is to escape the escape character.
So
>>print "\\hello"
\hello
Also as pointed by #IanPudney , python provides a special mechanism to print the raw string. Just use print r"\hello"
I hope this helps

Why is python automatically putting a \ infront of '? [duplicate]

This question already has answers here:
Why do backslashes appear twice?
(2 answers)
Closed 7 years ago.
Pretty much as question states- I have a code that finds sentences in a big string using regex- findall(). It then uses this sentence later, however when it uses it it puts a backslash infront of any apostrophe, for example Today's becomes Today\'s. Why is this happening, and how can I stop this happening?
It's called escaping a string. When you use " or ' inside of a string use \ to avoid lexical syntax errors. I believe there is a method that removes the escape character from a string if that's what you'd like to do.
The backslash denotes a so called escape sequence, which basically tells python that this character has to be interpreted differently from a "normal" ' character (which would signal the beginning or end of a string for the interpreter).

How to keep new line character \n in list element? [duplicate]

This question already has answers here:
Why do backslashes appear twice?
(2 answers)
Closed 8 years ago.
I am currently solving Project Euler 59. I take given ASCII sequence and xor it with 3 letter converted ASCII key. When I xor it and 2 elements in a row by chance are \ and n then my result becomes like that:
\\n instead of \n
As far as I understand it puts extra backslash to ignore new line, but I need my list exactly as it is for obvious reason. How can I have \n in my list without it becoming new line?
EDIT: now I double checked, everytime xor result becomes \ , it changes to \\ automatically.
The '\\n' you are seeing is actually a '\n'. The '\\' is just the way a backslash is escaped in strings, and as Joran points out, is a single character. For more information on escape sequences in Python strings, see https://docs.python.org/2/reference/lexical_analysis.html#string-literals
>>> s = '\\n'
>>> print s
\n
>>> print repr(s)
'\\n'
I think you misunderstand "\\" is one character:
print len("\\")
print ord("\\")

Categories