How do you write percentages in python?
this is my code: Show
and it shows:
but I want to get What can I do?
martineau 115k25 gold badges160 silver badges284 bronze badges asked Mar 15, 2011 at 2:10
3
If you don't want integer division, you can import Python3's division from
maxymoo 33.7k9 gold badges90 silver badges115 bronze badges answered Mar 15, 2011 at 2:16
mikumiku 175k46 gold badges303 silver badges307 bronze badges 7 There is a way more convenient 'percent'-formatting option for the
answered May 20, 2014 at 16:00 5 Just for the sake of completeness, since I noticed no one suggested this simple approach:
Details:
answered Aug 15, 2013 at 10:52
MestreLionMestreLion 11.9k4 gold badges62 silver badges55 bronze badges 6 Just to add Python 3 f-string solution
answered Sep 24, 2019 at 21:48
menrfamenrfa 1,25711 silver badges13 bronze badges 4 You are dividing integers then converting to float. Divide by floats instead. As a bonus, use the awesome string formatting methods described here: http://docs.python.org/library/string.html#format-specification-mini-language To specify a percent conversion and precision.
A great gem! answered Mar 15, 2011 at 2:16
Then you'd want to do this instead:
The answered Mar 15, 2011 at 2:16
3 I use this
answered Aug 19, 2021 at 8:16
DGKangDGKang 16112 bronze badges 1 this is what i did to get it working, worked like a charm
answered Jul 11 at 20:59
What is %s and %D in Python?%s is used as a placeholder for string values you want to inject into a formatted string. %d is used as a placeholder for numeric or decimal values.
What is %s in Python print?%s acts as a placeholder for the real value. You place the real value after the % operator. This method is often referred to as the "older" way because Python 3 introduced str. format() and formatted string literals (f-strings).
What is the percent symbol in Python?The % symbol in Python is called the Modulo Operator. It returns the remainder of dividing the left hand operand by right hand operand. It's used to get the remainder of a division problem. The modulo operator is considered an arithmetic operation, along with + , - , / , * , ** , // .
How do I write a percent in a string in Python?The Literal Percent Character ( %% )
To insert a percent character ( % ) into the output, you can specify two consecutive percent characters ( %% ) in the format string. The first percent character introduces a conversion specifier, and the second percent character specifies that the conversion type is % .
|