raw string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc Petitmermet

    raw string

    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;ge r"
    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
  • David C. Fox

    #2
    Re: raw string

    Marc Petitmermet wrote:[color=blue]
    > 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;ge r"
    > r"name".replace ('\\','&#')
    > -> 'r"name"[/color]

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

    If you say name = "kr\xdf6;ge r" 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

    Comment

    • Patrick Useldinger

      #3
      Re: raw string

      On Sat, 20 Sep 2003 21:10:46 +0200, Marc Petitmermet
      <petitmermet@ma t.ethz.ch> wrote:
      [color=blue]
      >I can do the following replacement:
      >
      >r"kr\xdf6;ger" .replace('\\',' &#')
      >-> 'kr&#xdf6;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;ge r"
      >r"name".replac e('\\','&#')
      >-> 'r"name"[/color]

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

      -PU

      Comment

      Working...