46

Possible Duplicates:
Difference between the use of double quote and quotes in python
Single quotes vs. double quotes in Python

So I am now learning python, and was creating a function. What is the difference between using ' and ". I will create a sample function below to exemplify my question.

def question(variable):
    print variable

now what is the difference between calling

question("hello")

and

question('hello')

they both print hello, but why can I use both? Is it just because python is flexible? I was confused because ' is usually used for chars where as " is for strings for java right?

codeforester
  • 39,467
  • 16
  • 112
  • 140
mergesort
  • 5,087
  • 13
  • 38
  • 63
  • 1
    and http://stackoverflow.com/questions/56011/single-quotes-vs-double-quotes-in-python – carl Feb 23 '11 at 06:02
  • If voting to close as a duplicate, please vote to close on the question linked by carl and not the original possible duplicate (which is itself just a "duplicate"). –  Feb 23 '11 at 06:57

3 Answers3

59

Both are equal and what you use is entirely your preference.

As far as the char vs string thing is concerned, refer to the Zen of Python, (PEP 20 or import this)

Special cases aren't special enough to break the rules.

A string of length 1 is not special enough to have a dedicated char type.

Note that you can do:

>>> print 'Double" quote inside single'
Double" quote inside single
>>> print "Single' quote inside double"
Single' quote inside double
user225312
  • 126,773
  • 69
  • 172
  • 181
  • im not a smart man, but i know not to question a guy with roughly 200x my reputation score. having said that, does this remain true if you are going between languages (e.g. into bash?)? – Vincent Laufer Aug 23 '22 at 21:02
17

" is useful when you have ' into the string and vice versa

luc
  • 41,928
  • 25
  • 127
  • 172
6

Python does not have that restriction of single quotes for chars and double quotes for strings.

As you can see here the grammar explicitly allows both for strings.

http://docs.python.org/reference/lexical_analysis.html#string-literals

John Weldon
  • 39,849
  • 11
  • 94
  • 127