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

31,917

Solution 1

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.

Solution 2

>>> variable = "Blah"
>>> '%s %%' % variable
    'Blah %'
>>> 

Solution 3

an easy way:

print ("One " + variable)
Share:
31,917
Oughh
Author by

Oughh

Updated on July 12, 2022

Comments

  • Oughh
    Oughh almost 2 years

    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)
    
  • Artyer
    Artyer over 8 years
    Or just print("One", variable) which works for both strings and non strings, but only in Python 3.x
  • Lutz Prechelt
    Lutz Prechelt about 8 years
    Here is the Python documentation on this and also a very nice tutorial. The tutorial also describes the newer, format() style of formatting, but the older % style is still much used because, although less powerful, it is often more convenient.
  • ryyker
    ryyker over 7 years
    Curious, what version of python did you use for this to work, 3x or 2x? (I am using 2.7.13, where it does not seem to work)
  • Aurora0001
    Aurora0001 over 7 years
    @ryyker, I just tested with Python 3.5.2 - works fine. As far as I can tell it should also work for 2.7, according to this.
  • pocpoc47
    pocpoc47 about 7 years
    this doesn't really answer the question but actually solves the problem I had when stumbling upon this question