1

I am new to programming and coding, and I choose Python to be my first. When I declare a variable, I notice that the variable = "x" and variable ='x' are the same. So can anyone told me the differences of "x" and 'x'

sophros
  • 14,672
  • 11
  • 46
  • 75
zinhdragon
  • 11
  • 1
  • 2

2 Answers2

4

According to PEP 8(Python Coding standard)

In Python, single-quoted strings and double-quoted strings are the same. This PEP does not make a recommendation for this. Pick a rule and stick to it. When a string contains single or double quote characters, however, use the other one to avoid backslashes in the string. It improves readability. For triple-quoted strings, always use double quote characters to be consistent with the docstring convention in PEP 257.

e.g ‘single-quote’ is the same as “single-quote” You will need a double quote when you need apostrophe in the middle of the string e.g “I’m a good boy”

3

In Python there is no difference between both quotations of a string. As you have noticed both result in the same value stored in a variable.

There is an important advantage of being able to use these quotation marks interchangeably - you can include in the string the quotation mark that you are not using to enquote the string, e.g.:

single = "This string variable contains 'single' quotation marks."
double = 'This string variable contains "double" quotation marks.'
sophros
  • 14,672
  • 11
  • 46
  • 75