Escaping the semicolon?

N

Nick

Hi all,

Is this expected behavior?
'123\\;abc'

I just wanted a single backslash. I can see why this probably happens
but i wondered if it is definitely intentional.

Thanks
Nick
 
B

Bruno Desthuilliers

Nick a écrit :
Hi all,

Is this expected behavior?
123\;abc

I just wanted a single backslash.

You got it - even if it's not obvious !-)
I can see why this probably happens
but i wondered if it is definitely intentional.
['1', '2', '3', '\\', ';', 'a', 'b', 'c']

As you can see, '\\' is counted as a single character !-)
Since the backslash is the escape character, you need to escape it to
have a litteral backslash:
File "<stdin>", line 1
s3 = '\'
^
SyntaxError: EOL while scanning single-quoted string
 
D

Diez B. Roggisch

Nick said:
Hi all,

Is this expected behavior?

'123\\;abc'

I just wanted a single backslash. I can see why this probably happens
but i wondered if it is definitely intentional.

There is only a single backslash. But the interactive prompt will use the
repr()-function to print out returned values. Which will for strings print
their escaped syntax.

Try the above with a

print s.replace(...)

and you will see your desired outcome.

diez
 
T

Tim Chase

Is this expected behavior?
'123\\;abc'

You're asking the interpreter to print a representation of your
string, so it does so. Representations wrap the results in
quotes and escape characters within that need escaping.
123\;abc

Additionally, it's best-practice to use raw strings or literal
backslashes, making your replacement either

r'\;'

or

'\\;'

because depending on the character following the back-slash, it
may be translated as a character you don't intend it to be.

-tkc
 
M

Mel

Nick said:
Is this expected behavior?

'123\\;abc'

I just wanted a single backslash. I can see why this probably happens
but i wondered if it is definitely intentional.

What you're seeing on the screen is a "literalization" of the string
value for the sake of the display. Consider

Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.8


The length suggests that there's only one backslash in the string.


Mel.

On the other hand
"'123\\\\;abc'"

Isn't what I expected. No, wait, it is. It's the value of repr(b)
repred by the Python display logic.

MPW
 
N

Nick

Thanks guys, you answered that interactive prompt question really
clearly however, whats going on here. This works now -
123\;abc

But this doesn't -

---
import sys
import string

input = string.join(sys.argv[1:], '')
escape_char_list = [ '(', '[]', ';', '^', '\\', '/', '|', '*', '$',
'&', '[', ']', ')', '?' ]

for ch in escape_char_list:
input = input.replace(ch, '\\' + ch)

print input
---

Try "123 *?/ abc d;o /$'" as the argument... and you get -

123 \*\?\/ abc d\\;o \/\$

Still two back slashes, did i miss something very obvious? Sorry,
sometimes these things are exasperating and just need more eyes or a
head screwed on (if thats the case!).

Nick
 
J

Jerry Hill

Try "123 *?/ abc d;o /$'" as the argument... and you get -

123 \*\?\/ abc d\\;o \/\$

That's because of the order you're doing the replacement. Put a print
statement inside your for loop and you'll see something like this:

input starts as "123 *?/ abc d;o /$'"
Then when you replace ';' with '\;' you get input = "123 *?/ abc d\;o /$'"
Then the next replacement replaces '\' with '\\' so you get input =
"123 *?/ abc d\\;o /$'"

If you move '\\' to the front of your list of replacement characters,
things will probably work as you expect.
 
N

Nick

If you move '\\' to the front of your list of replacement characters,
things will probably work as you expect.

I knew it would be something like that! Thanks for your help.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top