15. String Formatting w/ %


#1

I don't know what's wrong. The Error code shows up as Oops, try again. Make sure to use a lowercase "s" with \%s. Python told us about the following error: unsupported format character ',' (0x2c) at index 21.

My code.
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 %, your quest is %, " \
"and your favorite color is %." % (name, quest, color)


#2

In this bit of code:

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

The '%' symbols should be '%s', indicating a string. Try changing that and see if it works.


#3

@tomnomnom

Hi!

I have a doubt: Can we use %x or any other letter to have the same effect when we replace a value on a string?

Best,

Cobix


#4

@jlcobix
Hi, the character before the '%' defines the datatype of the variable that you want to print. We use '%s' because we want to print a string, but there are some other datatypes, for example:

-'%i' prints the output as an integer.
-'%f' prints the output as a float.
-'%s' print the output as a string.

For a complete list of the arguments go to https://docs.python.org/2/library/stdtypes.html#string-formatting and look at the section 5.6.2 String Formatting Operations