In need of help with string formatting with %, please


#1

name = raw_input("What is your name?")
quest = raw_input("What is your quest?")
color = raw_input("What is your favorite color?")

print "Ah, so your name is "%"_(venkat)__, your quest is "%" (music)___, " \
"and your favorite color is "%"_(black)__." %___ ("venkat", "music", "black")


#2

It's important that we attach a type indicator to the % inside the quotes of the string.

"This is a %s %s with %d wheels" % ('red', 'wagon', 4)

"  enclosing string expression " % ( argument tuple  )

Types

s => string
d => digit
f => float
r => object representation

The above are the most typical, but the list is not exhaustive.


#3

I didn't get you sir,can you do it in the above example,so that I can understand.


#4

Given the values,

name =  "Venkat"
quest = "music"
color = "black"

the string format expression will look like this...

print "Ah, so your name is %s, your quest is %s, \
and your favorite color is %s." %  (name, quest, color)

In the above, the continuation character is written inside the string. However, we can write it as two quoted strings, as well...

print ("Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." %  (name, quest, color))

Output

Ah, so your name is Venkat, your quest is music, and your favorite color is black.

#5

File "python", line 6
"and your favorite color is _%s." ___% (name, quest, color))
^
SyntaxError: invalid syntax


#6

Not enought information to go by. Please show your code. Be sure to remove all the underscore characters from the setup.


#7

It's done,Thank you sir


#8

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