Single versus double quotes for variables

What if we used double quotes for the string for name , instead of single quotes? What is the output of the Python code below?

1
2
3
4
5
6
name = "Aditya"
score = 98

message = "Hi %s. You achieved a score of %d in the class." % (name, score)

print message

Hi "Aditya". You achieved a score of 98 in the class. SyntaxError: invalid syntax Hi Aditya. You achieved a score of 98 in the class. TypeError: 'str' object is not callable

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

Saurab Thakur
Jul 5, 2014

Double Quotes just makes adding single quotes in the string possible eg: "Hey how are you 'Saloni'." o/p = Hey how are you 'Saloni'. But since we don't add any single quote its printed as is

in c++ it is a syntax err,

Adrian Hansen - 6 years, 11 months ago

Log in to reply

True, I thought about C++!

Vaibhav Khulbe - 6 years ago

I tried this in c++ with proper formatting but its giving me syntax error as output

Ayush Sharma - 5 years, 10 months ago
Amy Cureton
Nov 28, 2014

Double quotes work the same as single quotes in Python, and in JavaScript, HTML, CSS, Java, along with many other languages. Just be careful not to confuse them together like this: 'Hi!"

remove java from your list, you are incorrect

James Gibson - 5 years, 4 months ago
Shawn Pereira
Sep 10, 2014

Python accepts ("") as well as ('') for assigning string values. So, no error is thrown and the program runs as expected.

Tom Dufall
Jul 26, 2014

In a large number of programming languages single and double quotes are interchangeable when using them to give a string, allowing one to be used by enclosing it in the others (or using an escape character)

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...