operate with python '\' special character in a string - python

I am trying to iterate in a string and find a character on it and delete it.
For example, my string is "HowAre\youDoing" and I want the string "HowAreyouDoing" back (without the character '\'. My Loop is:
for c in string:
if c == '\':
The Point is that '\' is a Special character and it doesn´t allow me to do it in this way. Does anybody knows how can I proceed?
thanks

In python, as in most programing languages, the backslash character is used to introduce a special character, like \n for newline or \t for tab (and several more).
If you initialize a string in python with \y, it will escape it automatically, since \y is not a valid special character and python assumes that you want the actual character \ which is escaped to \\:
>>> s = "HowAre\youDoing"
>>> s
'HowAre\\youDoing'
So, to replace it in your case, just do
>>> s.replace("\\", "")
'HowAreyouDoing'
If you'd like to replace special characters like the aforementioned, you would need to specify the respective special character with an unescaped "\":
>>> s = "HowAre\nyouDoing"
>>> s
'HowAre\nyouDoing'
>>> s.replace("\n", "")
'HowAreyouDoing'

You should escape the character
for c in string:
if c == '\\':

Related

python equal strings are not equal with new line character in it

I have a password. It contains newline (lets for now omit why) character and is:
"h2sdf\ndfGd"
This password is in dict my_dict. When I just print values of dict I get "\" instead of "" - "h2sdf\ndfGd"! Don't understand why.
When I get it and use it to authenticate to web server, it says that Authentication fails. When I try to compare:
my_dict["password"] == "h2sdf\ndfGd"
it returns False.
But when I try just print(my_dict["password"]) I get h2sdf\ndfGd which is identical, but for python it is not. Why? I am lost.
Check this:
>>> print("h2sdf\ndfGd")
h2sdf
dfGd
>>> print("h2sdf\\ndfGd")
h2sdf\ndfGd
You simply have to escape \n with a double \ backslash, to prevent it to become a newline.
Characters like tabs, newlines, which cannot be represented in a string are described using an escape sequence with a backslash.
In order to indicate that the backslash is not part of an escape sequence(\n, \t, ...), it must itself be escaped using another backslash: \\.
my_dict["password"] == "h2sdf\\ndfGd"
If you don't want to have to escape all your \, you can use a raw string instead.
Raw strings are prefixed with r or R, and treat backslashes \ as literal characters.
my_dict["password"] == r"h2sdf\ndfGd"

Replacing a string by a string with backslashes

I am creating a program that automatically generates my reports in LaTeX, where I have to escape special LaTeX characters. Basically, whenever I read $ or _ or %, etc, I have to replace it by \$, \_ and \%, respectively.
I naively tried to do mystring.replace('$','\$'), yet it adds a double backslash, as shown below:
my_text_to_parse = "$x^2+2\cdot x + 2 = 0$"
my_text_to_parse.replace('$','\$')
#=> "\\$x^2+2\cdot x + 2 = 0\\$"
Is there any way to avoid doubling escape characters?
You're seeing the double backslash because you're getting the representation of the string, not the output. In the representation, it prints an backslash because \ is a protected character and therefore must be escaped. This is because it is used in special characters (e.g. \t, \n) and usage might be confused.. When the string is actually printed or saved, those double backslashes should be printed properly as a single backslash.
For example, compare
print('\')
# SyntaxError: EOL while scanning string literal
to
print('\\')
# \
In the first string, the second quotation mark is being escaped by the backslash. This shows why you generally can't use raw backslashes in strings. In the second string, the second backslash is being escaped by the first. The two backslashes get interpreted as a single one.
print(repr('\\'))
# '\\'
But the representation of the second string still shows both backslashes. This behavior is the same as other special characters such as \n, where it can be a bit easier to see the issue. Just as \n is the special character that means line break, \\ is the special character that means single backslash.
print('hi\nmom')
# hi
# mom
print(repr('hi\nmom'))
# 'hi\nmom'
To actually answer your question, the way you're doing it should work properly, but you probably don't want to do it quite that way. This is because creating a string with '\$' doesn't make this escaping issue clear. It seems like it is a special character \$ in the same way that \n is a special character, but because there is no character defined like that, the python interpreter is smart enough to replace the single backslash with a double backslash. But you generally don't want to rely on that behavior.
A better way to do it is to explicitly escape the backslash with another one or to use a raw string, where no escaping is allowed. All of these will give the same result.
s = '$x^2+2\\cdot x + 2 = 0$'
print(s.replace('$', '\$')) # Technically works, but not as clear
# \$x^2+2\cdot x + 2 = 0\$
print(s.replace('$', '\\$')) # Escaping the backslash
# \$x^2+2\cdot x + 2 = 0\$
print(s.replace('$', r'\$')) # Using a raw string
# \$x^2+2\cdot x + 2 = 0\$
print re.sub(r"\$","\$",x)
You can try re.sub.It will give the expected result.

What does backward-slash b do in Python?

What is the purpose of backward-slash b in python? I ran print "\"foo\bar" in the Python interpreter and got this result:
>>> print "\"foo\bar"
"foar
See the string literal documentation:
\b ASCII Backspace (BS)
It produces a backspace character. Your terminal backspaced over the second o when printing that character.
The \b is a back space character
\b ASCII Backspace (BS)
If you want to print the string \foo\bar do this:
>>> print r"\foo\bar"
\foo\bar
This utilizes the raw strings available in python.
String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences

How can I print a single backslash?

When I write print('\') or print("\") or print("'\'"), Python doesn't print the backslash \ symbol. Instead it errors for the first two and prints '' for the third. What should I do to print a backslash?
This question is about producing a string that has a single backslash in it. This is particularly tricky because it cannot be done with raw strings. For the related question about why such a string is represented with two backslashes, see Why do backslashes appear twice?. For including literal backslashes in other strings, see using backslash in python (not to escape).
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.
A hacky way of printing a backslash that doesn't involve escaping is to pass its character code to chr:
>>> print(chr(92))
\
print(fr"\{''}")
or how about this
print(r"\ "[0])
For completeness: A backslash can also be escaped as a hex sequence: "\x5c"; or a short Unicode sequence: "\u005c"; or a long Unicode sequence: "\U0000005c". All of these will produce a string with a single backslash, which Python will happily report back to you in its canonical representation - '\\'.

Replace '\' character in python

I don't know why i can't find it, but i wanted to replace the special character '\' in python.
I have a String within i have '\' characters but i confident find the solution, to replace it with '-'.
This is what happening while i am trying to replace,
>>> x = 'hello\world'
>>> x
'hello\\world'
>>> x.replace('\', '-')
File "<stdin>", line 1
x.replace('\', '-')
SyntaxError: EOL while scanning string literal
EDIT:
Do try this it in the eclipse IDLE
x = 'hello\world'
print x
x.replace('\\', '-')
print x
Output:
hello\world
hello\world
You need to escape it with another backslash:
x.replace('\\', '-')
Backslashes are special, in that they are used to introduce non-printing characters like newlines into a string.
It's also how you add a ' character to a '-quoted string, which is what Python thinks you were trying to do. It sees \' and interprets as a literal quote within the string, rather than letting the ' end the string. Then it gets to the end of the string and finds EOL ("end of line") before the end of the string.
To introduce a real backslash, you need to double it. You can see that Python itself did this when printing the representation of your initial string here:
>>> x
'hello\\world'
Note the double backslash.
You ought to use a double backslash when specifying your string in the first place. The reason that doesn't need it is that \w is not a special character, so it gets interpreted as a literal backslash and a w. Had you said 'Hello\now' you would have a string with a newline in it.
You could have also marked the string as a "raw" string by prepending it with r as in r'hello\world'. This marks the string as not being eligible for any substitutions of special characters.
According to docs:
The backslash (\) character is used to escape characters that
otherwise have a special meaning, such as newline, backslash itself,
or the quote character.
You need to escape backslash with another backslash:
x.replace('\\', '-')
This \' is interpreted as a special character. Escape it:
x.replace('\\', '-')
in python your string
x = 'hello\world'
is replaced as x = "hello\world"
so to achieve u have to write
x.replace('\\','-')

Categories