Printing variables in Python 3.4

Printing variables in Python 3.4

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.

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

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

Printing variables in Python 3.4

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.

Leave a Reply

Your email address will not be published.