Reputation: 65
I'm using Python 3.7.1 on Windows. When I print a float variable using the % formatting. only the natural part is printed. Here is an example:
result_2 = 4.523529411764706
statement_2a = "Your text contains an average length of %d letter(s) per words." % result_2
print(result_2)
print(statement_2a)
This gives the following result:
4.523529411764706
Your text contains an average length of 4 letter(s) per words.
But what I actually want is:
Your text contains an average length of 4.5235 letter(s) per words.
Thanks in advance!
Upvotes: 3
Views: 40090
Reputation: 2580
There are several options to evaluate expressions and print them as a string in python.
There are already some good answers, but here are some explicit examples and links to the documentation.
Formatted string literals (f-strings)
f-strings allow you to input expressions which are evaluated at run-time. In the f strings expressions are encased by curly brackets.
As an example:
x = 42.222222222
print(f'My value is: {x}')
prints My value is: 42.222222222
.
and with specifying the format:
x = 42.222222222
print(f'My value is: {x:.2f}')
prints My value is: 42.22
.
Str formatting method
Strings have a built-in .format()
method where you can specify replacement fields with curly brackets.
As an example:
x = 42.222222222
print('My value is: {}'.format(x))
prints My value is: 42.222222222
.
and with string formatting:
x = 42.222222222
print('My value is: {:.2f}'.format(x))
prints My value is: 42.22
.
String formatting operator
As an example:
x = 42.222222222
print('My value is: %' % x)
prints My value is: 42.222222222
.
and with string formatting:
x = 42.222222222
print('My value is: %.2f' % x)
prints My value is: 42.22
.
See @Felk answer for some more qualitive descriptions of the different methods.
Upvotes: 10
Reputation: 8224
You are using "old-style" formatting and used %d
as the placeholder. This will represent your number as a decimal number without any fractions. If you want to display floating point numbers, the placeholder is simply %f
.
If you want to use the variable's string representation, you can also always just use %s
. But since you are on python 3.7, there are some more modern approaches as well.
"%s" % var
format()
: "{}".format(var)
(Read up on python formatters for details)format()
-based string interpolation. You prefix with f
and put the variables in the string literal itself: f"{var}"
Upvotes: 1
Reputation: 2334
A better alternative than %
formatting would be to use .format
.
result_2 = 4.523529411764706
print(result_2)
print("Your text contains an average length of {} letter(s) per words.".format(result_2))
For rounding use round
result_2 = 4.523529411764706
print(result_2)
print("Your text contains an average length of {} letter(s) per words.".format(round(result_2,4)))
But if you feel comfortable with %
formatting then to print whole number use %s
result_2 = 4.523529411764706
statement_2a = "Your text contains an average length of %s letter(s) per words." % result_2
print(result_2)
print(statement_2a)
For rounding to 4 digits use %1.4f
result_2 = 4.523529411764706
statement_2a = "Your text contains an average length of %1.4f letter(s) per words." % result_2
print(result_2)
print(statement_2a)
Upvotes: 1
Reputation: 714
As you try print float number, use %f instead of %d. This code will print the number to 4 decimal places:
result_2 = 4.523529411764706
statement_2a = "Your text contains an average length of %.4f letter(s) per words." % result_2
print(result_2)
print(statement_2a)
Upvotes: 3