Is It Bug?

M

Mahan Marwat

Why this is not working.

To me, Python will interpret '\\\\' to '\\'. And the replace method will replace '\\' with '\'. So, the result will be 'Hello, \World'. But it's give me 'Hello, \\\\World'.

The result I want form the code is 'Hello, \World'.
 
C

Chris Angelico

Why this is not working.


To me, Python will interpret '\\\\' to '\\'. And the replace method will replace '\\' with '\'. So, the result will be 'Hello, \World'. But it's give me 'Hello, \\\\World'.

The result I want form the code is 'Hello, \World'.

You're replacing with the same as the source string. That's not going
to change anything.

The first thing to get your head around is Python string literals.
You'll find them well described in the online tutorial, or poke around
in the interactive interpreter. Once you master that, you should be
able to understand what you're trying to do here.

ChrisA
 
M

MRAB

Why this is not working.


To me, Python will interpret '\\\\' to '\\'. And the replace method
will replace '\\' with '\'. So, the result will be 'Hello, \World'.
But it's give me 'Hello, \\\\World'.

The result I want form the code is 'Hello, \World'.
The original string contains 2 actual backslashes:
Hello, \\World

Both the search and replacement strings contain 1 backslash:
\

You're asking it to replace every backslash with a backslash!

If you want to replace 2 consecutive backslashed with a single
backslash:
'Hello, \\World'

Maybe it's clearer if you print it:
Hello, \World
 
R

Roy Smith

Chris Angelico said:
The first thing to get your head around is Python string literals.
You'll find them well described in the online tutorial, or poke around
in the interactive interpreter.

A couple of ideas to explore along those lines:

1) Read up on raw strings, i.e.

r'Hello, \World'

instead of

'Hello, \\Word'

There's nothing you can do with raw strings that you can't do with
regular strings, but they're easier to read when you start to use
backslashes.

2) When in doubt about what I'm looking at in a string, I turn it into a
list. So, if I do:
Hello, \World

What is that character after the space? Is it a backslash, or is it
something that Python is printing as \W? Not sure? Just do:
['H', 'e', 'l', 'l', 'o', ',', ' ', '\\', 'W', 'o', 'r', 'l', 'd']

and it's immediately obvious which it is.
 
T

Tim Roberts

Mahan Marwat said:
Why this is not working.


To me, Python will interpret '\\\\' to '\\'.

It's really important that you think about the difference between the way
string literals are written in Python code, and the way the strings
actually look in memory.

The Python literal 'Hello, \\\\World' contains exactly 2 backslashes. We
have to spell it with 4 backslashes to get that result, but in memory there
are only two.

Similarly, the Python literal '\\' contains exactly one character.

So, if your goal is to change 2 backslashes to 1, you would need
'Hello, \\\\World'.replace('\\\\','\\')

However, REMEMBER that if you just have the command-line interpreter echo
the result of that, it's going to show you the string representation, in
which each backslash is shown as TWO characters. Observe:

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 
C

Chris “Kwpolska†Warrick

There's nothing you can do with raw strings that you can't do with
regular strings, but they're easier to read when you start to use
backslashes.

Unfortunately, there is one. A raw string cannot end with a backslash.
File "<stdin>", line 1
r'a\'
^
SyntaxError: EOL while scanning string literalFile "<stdin>", line 1
r'\'
^
SyntaxError: EOL while scanning string literal
 
C

Chris Angelico

Unfortunately, there is one. A raw string cannot end with a backslash.

That's the other way around. There's something you can't do with a raw
string that you can do with a regular. But there's nothing you can do
with a raw that you can't do with a regular, as can be easily proven
by looking at the repr handling - nothing will ever have a repr that's
a raw string.

ChrisA
 
P

Peter Otten

Mahan said:
Why this is not working.


To me, Python will interpret '\\\\' to '\\'. And the replace method will
replace '\\' with '\'. So, the result will be 'Hello, \World'. But it's
give me 'Hello, \\\\World'.

The result I want form the code is 'Hello, \World'.

Let's forget about backslashes for the moment and use 'a' instead. We can
replace an 'a' with an 'a'
'Hello, aaWorld'

That changes nothing. Or we can replace two 'a's with one 'a'
'Hello, aWorld'

This does the obvious thing. Finally we can replace an 'a' with the empty
string '':
'Hello, World'

This effectively removes all 'a's.

Now let's replace the "a" with a backslash. Because the backslash has a
special meaning it has to be "escaped", i. e. preceded by another backslash.
The examples then become
'Hello, World'

While doubling of backslashes is required by Python the doubling of
backslahses in the output occurs because the interactive interpreter applies
repr() to the string before it is shown. You can avoid that with an explicit
print statement in Python 2 or a print() function call in Python 3:
Hello, World
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,184
Latest member
ZNOChrista

Latest Threads

Top