raw string

M

Marc Petitmermet

I can do the following replacement:

r"kr\xdf6;ger".replace('\\','&#')
-> 'kr෶ger'

But how can I do this using a variable which contains the above string?
Obviously, the following code returns not the string with replacement
but the name of the variable itself:

name = "kr\xdf6;ger"
r"name".replace('\\','&#')
-> 'r"name"

Sorry, if this is a stupid question but I have not found an answer so
far (or I have been using the wrong keywords for searching).

Thanks for any help.
Regards,
Marc
 
D

David C. Fox

Marc said:
I can do the following replacement:

r"kr\xdf6;ger".replace('\\','&#')
-> 'kr෶ger'

But how can I do this using a variable which contains the above string?
Obviously, the following code returns not the string with replacement
but the name of the variable itself:

name = "kr\xdf6;ger"
r"name".replace('\\','&#')
-> 'r"name"

name = r"kr\xdf6;ger"
name.replace('\\', '&#')

If you say name = "kr\xdf6;ger" without the r prefix, the \x has already
been replaced with a single character, and there is no slash in the
string, so it is too late to try to replace a slash with something else.

David
 
P

Patrick Useldinger

I can do the following replacement:

r"kr\xdf6;ger".replace('\\','&#')
-> 'kr෶ger'

But how can I do this using a variable which contains the above string?
Obviously, the following code returns not the string with replacement
but the name of the variable itself:

name = "kr\xdf6;ger"
r"name".replace('\\','&#')
-> 'r"name"

name = r"kr\xdf6;ger" (the r goes here)
name.replace('\\','&#') (the name of the object must not be in quotes)

-PU
 

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
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top