raw string

David C. Fox davidcfox at post.harvard.edu
Sat Sep 20 15:29:30 EDT 2003


Marc Petitmermet wrote:
> 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





More information about the Python-list mailing list