Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python "ValueError: incomplete format" upon print("stuff %" % "thingy")

My goal with this code is that when you put in a certain number, you will get printed the number and some other output, based on what you typed. For some reason, what I have here gives the error "ValueError: incomplete format". It has something to do with the %. What does the error mean, and how do I fix it? Thanks!

variable = "Blah"
variable2 = "Blahblah"

text = raw_input("Type some stuff: ")

if "1" in text:
    print ("One %" % variable)
elif "2" in text:
    print ("Two %" % variable2)
like image 881
Oughh Avatar asked Jan 07 '16 22:01

Oughh


3 Answers

>>> variable = "Blah"
>>> '%s %%' % variable
    'Blah %'
>>> 
like image 98
Ravi Gadhia Avatar answered Nov 07 '22 20:11

Ravi Gadhia


Python is expecting another character to follow the % in the string literal to tell it how to represent variable in the resulting string.

Instead use

"One %s" % (variable,)

or

"One {}".format(variable)

to create a new string where the string representation of variable is used instead of the placeholder.

like image 23
Chad S. Avatar answered Nov 07 '22 20:11

Chad S.


an easy way:

print ("One " + variable)
like image 1
ᴀʀᴍᴀɴ Avatar answered Nov 07 '22 21:11

ᴀʀᴍᴀɴ