Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is It Bug?

Reply
Thread Tools

Is It Bug?

 
 
Mahan Marwat
Guest
Posts: n/a
 
      12-08-2013
Why this is not working.

>>> 'Hello, \\\\World'.replace('\\', '\\')


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'.
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      12-08-2013
On Sun, Dec 8, 2013 at 11:59 AM, Mahan Marwat <(E-Mail Removed)> wrote:
> Why this is not working.
>
>>>> 'Hello, \\\\World'.replace('\\', '\\')

>
> 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
 
Reply With Quote
 
 
 
 
Mark Lawrence
Guest
Posts: n/a
 
      12-08-2013
On 08/12/2013 00:59, Mahan Marwat wrote:
> Why this is not working.
>
>>>> 'Hello, \\\\World'.replace('\\', '\\')


Whoops a daisy!!! --------------^^^^--^^^^ ???

>
> 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'.
>



--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      12-08-2013
On 08/12/2013 00:59, Mahan Marwat wrote:
> Why this is not working.
>
>>>> 'Hello, \\\\World'.replace('\\', '\\')

>
> 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:

>>> print('Hello, \\\\World')

Hello, \\World

Both the search and replacement strings contain 1 backslash:

>>> print('\\')

\

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'.replace('\\\\', '\\')

'Hello, \\World'

Maybe it's clearer if you print it:

>>> print('Hello, \\\\World'.replace('\\\\', '\\'))

Hello, \World
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      12-08-2013
In article <(E-Mail Removed)>,
Chris Angelico <(E-Mail Removed)> wrote:

> 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:

>>> s = 'Hello, \\World'
>>> print s

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:

>>> print list(s)

['H', 'e', 'l', 'l', 'o', ',', ' ', '\\', 'W', 'o', 'r', 'l', 'd']

and it's immediately obvious which it is.
 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      12-08-2013
Mahan Marwat <(E-Mail Removed)> wrote:
>
>Why this is not working.
>
>>>> 'Hello, \\\\World'.replace('\\', '\\')

>
>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.
>>> s = 'Hello, \\\\World'
>>> s

'Hello, \\\\World'
>>> print s

Hello, \\World
>>> s = s.replace('\\\\','\\')
>>> s

'Hello, \\World'
>>> print s

Hello, \World
>>>

--
Tim Roberts, http://www.velocityreviews.com/forums/(E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
Chris “Kwpolska” Warrick
Guest
Posts: n/a
 
      12-08-2013
On Sun, Dec 8, 2013 at 2:22 AM, Roy Smith <(E-Mail Removed)> wrote:
> 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.

>>> r'a\a'

'a\\a'
>>> r'a\'

File "<stdin>", line 1
r'a\'
^
SyntaxError: EOL while scanning string literal
>>> r'\'

File "<stdin>", line 1
r'\'
^
SyntaxError: EOL while scanning string literal

--
Chris “Kwpolska” Warrick <http://kwpolska.tk>
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      12-08-2013
On Sun, Dec 8, 2013 at 9:01 PM, Chris “Kwpolska” Warrick
<(E-Mail Removed)> wrote:
> On Sun, Dec 8, 2013 at 2:22 AM, Roy Smith <(E-Mail Removed)> wrote:
>> 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.


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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      12-08-2013
Mahan Marwat wrote:

> Why this is not working.
>
>>>> 'Hello, \\\\World'.replace('\\', '\\')

>
> 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".replace("a", "a")

'Hello, aaWorld'

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

>>> "Hello, aaWorld".replace("aa", "a")

'Hello, aWorld'

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

>>> "Hello, aaWorld".replace("a", "")

'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".replace("\\", "\\")

'Hello, \\\\World'
>>> "Hello, \\\\World".replace("\\\\", "\\")

'Hello, \\World'
>>> "Hello, \\\\World".replace("\\", "")

'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:

>>> print "Hello, \\\\World".replace("\\", "\\")

Hello, \\World
>>> print "Hello, \\\\World".replace("\\\\", "\\")

Hello, \World
>>> print "Hello, \\\\World".replace("\\", "")

Hello, World

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
*bug* *bug* *bug* David Raleigh Arnold Firefox 12 04-02-2007 03:13 AM
ASP.NET Login control bug or SQL 2005 bug? RedEye ASP .Net 2 12-13-2005 10:57 AM
Re: BUG? OR NOT A BUG? John ASP .Net 2 09-21-2005 10:31 AM
Bug Parade Bug 4953793 Michel Joly de Lotbiniere Java 4 12-02-2003 05:05 AM
how to report bug to g++ ? got a bug and fixed up source code DarkSpy C++ 4 06-27-2003 09:05 AM



Advertisments