[Python] String pattern substitution in Python

Discussão em 'Python' iniciado por Stack, Junho 6, 2025.

  1. Stack

    Stack Membro Participativo

    I have a situation where I would like to rename a string as follows:

    1. Say I have a string x = "foo$"
    2. I want to rename this string as "foo\$"

    Unfortunately when I do this: x.replace("$", "\$") The new string becomes foo\\$ I need the literal to be recognized as a part of the pattern. I tried x.replace("$", "\\\$") but this gave the same result. How to fix this?

    Continue reading...

Compartilhe esta Página