Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 10 Online
OpenStudy (vaboro):

What is the difference b/w single quotes and double quotes? Why do I see them used interchangeably in numerous examples? When should single quotes be used and when double quotes?

OpenStudy (anonymous):

they accomplish the same function. it just depends on what youre trying to print. if youre trying to print something like. "He went to the store," said the boy. you would type print '"He went to the store", said the boy.' whenever you type the first quote it will look for the next instance of that particular character to terminate the string. so you can put double quotes inside single quote or single quotes inside double quotes. you can additionally use all the same kind of quotes and just put a "\" infront of the quotes you want to appear in the string. so the earlier example would be print "\"He went to the store,\" said the boy."

OpenStudy (carlsmith):

Nothing really. In Python, you can use either, but you must end the string with the same type of quotes as you start it with. It's useful because you'll often have quotes in strings. If you did name = 'O'Reilly' you'd get an error because the string 'O' is followed by some gibberish code, Reilly'. If you do name = "O'Reilly" everything is fine. You can use single quotes when you've got double quotes in a string. quote = 'He replies, "We're an autonomous workers collective".' If you want your string to span multiple lines, or if it contains both single and double quotes, you can use 'triple quotes', three single or three double quotes to open and three more of the same to close. both = ''' O'Reilly says "Hello" ''' long = ''' One line of text another one '''

OpenStudy (carlsmith):

Note: In triple quoted strings, newlines are preserved, even if they're blank lines. >>>print long One line of text another one >>> You've also got the escape characters. A backslashed character which has a special meaning. There's a few, but these are the common ones. \n = newline \t = tab \' = treat this single quote as a regular part of the string \" = same as above, but for a double quote \\ = same again, but for a backslash

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!