51

So the syntax seems to have changed from what I learned in Python 2... here is what I have so far

for key in word:
    i = 1
    if i < 6:
        print ( "%s. %s appears %s times.") % (str(i), key, str(wordBank[key]))

The first value being an int, the second a string, and the final an int.

How can I alter my print statement so that it prints the variables correctly?

4
  • 1
    print() is a function in Python 3, not a statement. Surround everything in your last line (except the word print) in parentheses and you'll be all set.
    – MattDMo
    Nov 14, 2014 at 21:03
  • 2
    @CMac: no, you are not. You did this: print(....), which will return None, then None % (one, two, three_strings). You want the something % (one, two, three_strings) to be done inside the print(....) call.
    – Martijn Pieters
    Nov 14, 2014 at 21:05
  • Read this for future reference...
    – MattDMo
    Nov 14, 2014 at 21:05
  • here is a good link: matthew-brett.github.io/teaching/string_formatting.html
    – leozilla
    Oct 30, 2018 at 17:13

6 Answers 6

135

The syntax has changed in that print is now a function. This means that the % formatting needs to be done inside the parenthesis:1

print("%d. %s appears %d times." % (i, key, wordBank[key]))

However, since you are using Python 3.x., you should actually be using the newer str.format method:

print("{}. {} appears {} times.".format(i, key, wordBank[key]))

Though % formatting is not officially deprecated (yet), it is discouraged in favor of str.format and will most likely be removed from the language in a coming version (Python 4 maybe?).


1Just a minor note: %d is the format specifier for integers, not %s.

1
  • 3
    The very clunky pre-Python 3.6 syntax has now been superseded with f-strings, as per wwii's answer. Could you please update your answer, it's obsolete?
    – smci
    Nov 28, 2018 at 2:51
61

Version 3.6+: Use a formatted string literal, f-string for short

print(f"{i}. {key} appears {wordBank[key]} times.")
0
9

Try the format syntax:

print ("{0}. {1} appears {2} times.".format(1, 'b', 3.1415))

Outputs:

1. b appears 3.1415 times.

The print function is called just like any other function, with parenthesis around all its arguments.

6

You can also format the string like so:

>>> print ("{index}. {word} appears {count} times".format(index=1, word='Hello', count=42))

Which outputs

1. Hello appears 42 times.

Because the values are named, their order does not matter. Making the example below output the same as the above example.

>>> print ("{index}. {word} appears {count} times".format(count=42, index=1, word='Hello'))

Formatting string this way allows you to do this.

>>> data = {'count':42, 'index':1, 'word':'Hello'}
>>> print ("{index}. {word} appears {count} times.".format(**data))
1. Hello appears 42 times.
1

The problem seems to be a mis-placed ). In your sample you have the % outside of the print(), you should move it inside:

Use this:

print("%s. %s appears %s times." % (str(i), key, str(wordBank[key])))
0

one can print values using the format method in python. This small example will help take input of two numbers a and b. Print a+b in first line and a-b in second line

print('{:d}\n{:d}'.format(a+b,a-b))

Similarly in the answer we can do

print ("{0}. {1} appears {2} times.".format(22, 'c', 9999))

The python method format() for string is used to specify a string format. So {0},{1},{2} are like array indexes called as positional parameters. Therefore {0} is assigned first value written in format (a+b), {1} is assigned the second value (a-b) and so on. We can also use keyword instead of positional parameter like for example

print("Hi! my name is {name}".format(name="rashi"))

Therefore name here is the keyword and its value is Rashi Hope it helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.