Printing variables in Python 3.4
Question:
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?
Answers:
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])))
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
.
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.
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.
Version 3.6+: Use a formatted string literal, f-string for short
print(f"{i}. {key} appears {wordBank[key]} times.")
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 🙂
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?
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])))
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
.
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.
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.
Version 3.6+: Use a formatted string literal, f-string for short
print(f"{i}. {key} appears {wordBank[key]} times.")
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 🙂