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

668
Vistas
Why doesn't Python give any error when quotes around a string do not match?

I've started learning Python recently and I don't understand why Python behaves like this:

>>> "OK"
'OK'
>>> """OK"""
'OK'
>>> "not Ok'
  File "<stdin>", line 1
    "not Ok'
           ^
SyntaxError: EOL while scanning string literal
>>> "not OK"""
'not OK'

Why doesn't it give an error for the last statement as the number of quotes does not match?

over 4 years ago · Hanz Gallego
4 Respuestas
Responde la pregunta

0

The final """ is not recognized as a triple-quotation, but a single " (to close the current string literal) followed by an empty string ""; the two juxtaposed string literals are concatenated. The same behavior can be more readily recognized by putting a space between the closing and opening ".

>>> "not OK" ""
'not OK'
over 4 years ago · Hanz Gallego Denunciar

0

"not OK"""

Python interprets this as "not OK"+""

If you give "not Ok""ay", you will get the output as 'not Okay'

over 4 years ago · Hanz Gallego Denunciar

0

You would think that there is no difference between " or ', but in reality, Python uses a greedy method to accept input.

Once Python sees a matching quotation, then that ends the statement.

It's why you can write something like "'s" "". Inside the string there is a ' but because you're in a string, python doesn't raise an error. Then after that, there is a " followed by " but that's a different (empty) string.

If you do something like "s' then Python is looking for that next " before if runs your command.

over 4 years ago · Hanz Gallego Denunciar

0

Python uses like a stack implementation to detect quotes opening and closing. If you know whats a stack is, its a datastructure, in which Last element will be removed first.

Assume your string is A = "''" What it does is, for every single quote or double quote encountered first time, it will add it the stack, and for every second, it will remove from the stack, unless its ofcourse, """ which will be parsed as single one

In our example, A = "''", iterating over it, for the first 2 elements, they will be added to stack and for the next 2, they will be removed.

So the quotes will be matched, if and only if, the number of elements in the stack in the end must be zero

over 4 years ago · Hanz Gallego 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