By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

Escaping the semicolon?

P: n/a
Hi all,

Is this expected behavior?
>>s = '123;abc'
s.replace(';', '\;')
'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
Dec 4 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Nick a écrit :
Hi all,

Is this expected behavior?
>>>s = '123;abc'
s.replace(';', '\;')
'123\\;abc'
>>print s.replace(';', '\;')
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.
>>s2 = '123\;abc'
s2
'123\\;abc'
>>print s2
123\;abc
>>list(s2)
['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:
>>s3 = '\'
File "<stdin>", line 1
s3 = '\'
^
SyntaxError: EOL while scanning single-quoted string
>>>
Dec 4 '07 #2

P: n/a
Nick wrote:
Hi all,

Is this expected behavior?
>>>s = '123;abc'
s.replace(';', '\;')
'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
Dec 4 '07 #3

P: n/a
Is this expected behavior?
>
>>>s = '123;abc'
s.replace(';', '\;')
'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.
>>s.replace(';', '\;')
'123\\;abc'
>>print repr(s.replace(';', '\;'))
'123\\;abc'
>>print s.replace(';', '\;')
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
Dec 4 '07 #4

P: n/a
Mel
Nick wrote:
Is this expected behavior?
>>>s = '123;abc'
s.replace(';', '\;')
'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.
>>s = '123;abc'
b = s.replace(';', '\;')
b
'123\\;abc'
>>len(b)
8
The length suggests that there's only one backslash in the string.
Mel.

On the other hand
>>repr(b)
"'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
Dec 4 '07 #5

P: n/a
Thanks guys, you answered that interactive prompt question really
clearly however, whats going on here. This works now -
>>working_string = '123;abc'
search_string = ';'
print working_string.replace(search_string, '\\' + search_string)
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
Dec 4 '07 #6

P: n/a
>
If you move '\\' to the front of your list of replacement characters,
things will probably work as you expect.

--
Jerry
I knew it would be something like that! Thanks for your help.
Dec 4 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.