Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

273
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

You can try this also

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

or

print l.replace('"','\\"')
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda