weird behaviour of quotes in python (NEED EXPLAINATION ASAP)

35
Jun 28, 2022
  • The thing I know about quotes in python is
    • '2', "2", """2"""
    • all these three syntaxes create a string and assign it a value 2
  • If one needs to include ""(double quotes) inside string, they can use ''(single quotes) to define string
    • a = '"2"'
    • print(a) --> "2"
  • same thing if one needs to include ''(single quotes) inside string, they can use ''(double quotes) to define string
    • a = "'2'"
    • print(a) --> '2'
  • similarily if one needs to include both ''(single quotes) and ""(double quotes) inside string, they can use """ or ''' (three single quotes or three double quotes) to define string

But how does these combinations work? need an explaination ASAP pls!! Thnks in advance

suppose,

a = ''"2"''
print(a) --> 2

why is the above result is like so?

and suppose,

a = ""'2'""
print(a) --> 2

why is the above result is like so?

and suppose,

a = '""'"2"'""'
print(a) --> ""2""

why is the above result like so?

Comments (2)