Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

274
Visualizações
replace double quotes with '\"' in python

I have a string -

l = '{"a": "1", "b": "2"}'

I want to convert this string to -

'{\"a\": \"1\", \"b\": \"2\"}'

For this I am trying to replace " with \"

Here's what I have tried -

l.replace('\"', '\"')
'{"a": "1", "b": "2"}' 

l.replace('\"', '\\"')
'{\\"a\\": \\"1\\", \\"b\\": \\"2\\"}'

How do I convert {\"a\": \"1\", \"b\": \"2\"}?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Try this:

print l.replace('"','\\"')

'\"' doesn't mean anything special to Python, so you needn't to add \ before ",if you run

print l.replace('\"', '\\"'),you will get a single backslash too.

Actually what you are seeing is the representation of the string, it's added by repr() method.Python represents backslashes in strings as \\ because the backslash is an Escape Character .

If you print it, you will get single backslash.

You can see more information from String and Bytes literals.

over 4 years ago · Santiago Trujillo Relatório

0

You can try this also

print l.replace('"',r'\"')

or

print l.replace('"','\\"')
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda