Input double quotes in a string


#1

Please leave the following link in the post so we can find the unit module, else replace it with a link to the exact lesson:

Strings and Console Output

Here is their code they are asking for three inputs from the user
name = raw_input("What is your name? ")
quest = raw_input("What is your quest? ")
color = raw_input("What is your favorite color? ")

and then they are printing it with placeholders %s

So when I’m inputting double quotes with the string it is not giving an error
What is your name? j"
What is your quest? k"
What is your favorite color? l"

String to print must be like this after input:
“Ah, so your name is j”, your quest is k", and your favorite color is l"
and this must gives an error, why it is not giving an error and printing exactly right ?


#2

Inside the string template we specify the datatype of the parameters. They will all be converted to string for output, hence the quotes around the whoe thing.

>>> "%s %d %.3f %r" % ("string", 3, 1.2479, [1,1,2,3,5,8])
'string 3 1.248 [1, 1, 2, 3, 5, 8]'
>>> 

#3

Ty mtf

I’m just confirming whether I understood it right or not.

So, you are saying, this string below is just a template

>>>"Hello %s Welcome to %s"
>>>

and this template is passed in print function

>>> print("Hello %s Welcome to %s" % ("Sharad Gaur", "Codecademy"))
>>>

print function, which converts the whole template to string to output it on the console.


#4

No, that would be like saying that print is doing addition here:

print 1 + 1

The addition happens, and print receives the result.

Also, you have two placeholders in there but only provide one value. That will raise an exception.


#5

% takes a single value or a tuple.


#6

This is what I mean.
Below is the final value after input is going into the template

if I bind this whole above text in double quotes, then how the print function is identifying the input

“Ah, so your name is j”, your quest is k", and your favorite color is l""

double quotes after j, k, and l are not string closing literal and they need to be escaped?


#7

We don’t make it a string; it is a string, only it has value placeholders which values are found in the arguments list following % outside of the string. It’s a template because it can interpolate those argument values. We tell the interpreter what data type to expect and it reads the values in, inserts them in the order they are witten into the next available placeholder slot, and outputs everything as one string.

print (“Ah, so your name is j”, your quest is k", and your favorite color is l")

Too many quotes in above. It will raise an undefined identifier error.

By rights it would be,

print (“Ah, so your name is %s, your quest is %s, and your favorite color is %s" % (j, k, l))

#8

print doesn’t do any of that.
It prints strings

You have to create the string before printing it, because print won’t alter it in any way.


#9

And on the above note, the string is the thing. We can either print it or store it (assign to a variable or data structure) in its interpreted form.


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.