this is my code:
Nội dung chính
- What does %s mean in Python?
- How do I convert a string to a percent in Python?
- What does %s mean in Python?
- How do I convert a string to a percent in Python?
print str[float[1/3]]+'%'
and it shows:
0.0%
but I want to get 33%
What can I do?
martineau
115k25 gold badges160 silver badges282 bronze badges
asked Mar 15, 2011 at 2:10
3
format
supports a percentage floating point precision type:
>>> print "{0:.0%}".format[1./3]
33%
If you don't want integer division, you can import Python3's division from __future__
:
>>> from __future__ import division
>>> 1 / 3
0.3333333333333333
# The above 33% example would could now be written without the explicit
# float conversion:
>>> print "{0:.0f}%".format[1/3 * 100]
33%
# Or even shorter using the format mini language:
>>> print "{:.0%}".format[1/3]
33%
maxymoo
33.5k9 gold badges89 silver badges115 bronze badges
answered Mar 15, 2011 at 2:16
mikumiku
175k46 gold badges302 silver badges307 bronze badges
7
There is a way more convenient 'percent'-formatting option for
the .format[]
format method:
>>> '{:.1%}'.format[1/3.0]
'33.3%'
answered May 20, 2014 at 16:00
5
Just for the sake of completeness, since I noticed no one suggested this simple approach:
>>> print["%.0f%%" % [100 * 1.0/3]]
33%
Details:
%.0f
stands for "print a float with 0 decimal places", so%.2f
would print33.33
%%
prints a literal%
. A bit cleaner than your original+'%'
1.0
instead of1
takes care of coercing the division to float, so no more0.0
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
prob = 1.0/3.0
print[f"{prob:.0%}"]
answered Sep 24, 2019 at 21:48
menrfamenrfa
1,23711 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: //docs.python.org/library/string.html#format-specification-mini-language
To specify a percent conversion and precision.
>>> float[1] / float[3]
[Out] 0.33333333333333331
>>> 1.0/3.0
[Out] 0.33333333333333331
>>> '{0:.0%}'.format[1.0/3.0] # use string formatting to specify precision
[Out] '33%'
>>> '{percent:.2%}'.format[percent=1.0/3.0]
[Out] '33.33%'
A great gem!
answered Mar 15, 2011 at 2:16
Then you'd want to do this instead:
print str[int[1.0/3.0*100]]+'%'
The .0
denotes them as floats and int[]
rounds them to integers afterwards again.
answered Mar 15, 2011 at 2:16
3
I use this
ratio = round[1/3, 2]
print[f"{ratio} %"]
output: 0.33 %
answered Aug 19, 2021 at 8:16
DGKangDGKang
16212 bronze badges
1
this is what i did to get it working, worked like a charm
divideing = a / b
percentage = divideing * 100
print[str[float[percentage]]+"%"]
answered Jul 11 at 20:59
What does %s mean in Python?
%s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string. It is used to incorporate another string within a string. It automatically provides type conversion from value to string.
How do I convert a string to a percent in Python?
To convert it back to percentage string, we will need to use python's string format syntax '{:. 2%}'. format to add the '%' sign back.
this is my code:
print str[float[1/3]]+'%'
and it shows:
0.0%
but I want to get 33%
What can I do?
martineau
115k25 gold badges160 silver badges282 bronze badges
asked Mar 15, 2011 at 2:10
3
format
supports a percentage floating point precision type:
>>> print "{0:.0%}".format[1./3]
33%
If you don't want integer division, you can import Python3's division from __future__
:
>>> from __future__ import division
>>> 1 / 3
0.3333333333333333
# The above 33% example would could now be written without the explicit
# float conversion:
>>> print "{0:.0f}%".format[1/3 * 100]
33%
# Or even shorter using the format mini language:
>>> print "{:.0%}".format[1/3]
33%
maxymoo
33.5k9 gold badges89 silver badges115 bronze badges
answered Mar 15, 2011 at 2:16
mikumiku
175k46 gold badges302 silver badges307 bronze badges
7
There is a way more convenient 'percent'-formatting option for the .format[]
format method:
>>> '{:.1%}'.format[1/3.0]
'33.3%'
answered May 20, 2014 at 16:00
5
Just for the sake of completeness, since I noticed no one suggested this simple approach:
>>> print["%.0f%%" % [100 * 1.0/3]]
33%
Details:
%.0f
stands for "print a float with 0 decimal places", so%.2f
would print33.33
%%
prints a literal%
. A bit cleaner than your original+'%'
1.0
instead of1
takes care of coercing the division to float, so no more0.0
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
prob = 1.0/3.0
print[f"{prob:.0%}"]
answered Sep 24, 2019 at 21:48
menrfamenrfa
1,23711 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: //docs.python.org/library/string.html#format-specification-mini-language
To specify a percent conversion and precision.
>>> float[1] / float[3]
[Out] 0.33333333333333331
>>> 1.0/3.0
[Out] 0.33333333333333331
>>> '{0:.0%}'.format[1.0/3.0] # use string formatting to specify precision
[Out] '33%'
>>> '{percent:.2%}'.format[percent=1.0/3.0]
[Out] '33.33%'
A great gem!
answered Mar 15, 2011 at 2:16
Then you'd want to do this instead:
print str[int[1.0/3.0*100]]+'%'
The .0
denotes them as floats and int[]
rounds them to integers afterwards again.
answered Mar 15, 2011 at 2:16
3
I use this
ratio = round[1/3, 2]
print[f"{ratio} %"]
output: 0.33 %
answered Aug 19, 2021 at 8:16
DGKangDGKang
16212 bronze badges
1
this is what i did to get it working, worked like a charm
divideing = a / b
percentage = divideing * 100
print[str[float[percentage]]+"%"]
answered Jul 11 at 20:59
What does %s mean in Python?
%s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string. It is used to incorporate another string within a string. It automatically provides type conversion from value to string.
How do I convert a string to a percent in Python?
To convert it back to percentage string, we will need to use python's string format syntax '{:. 2%}'. format to add the '%' sign back.
this is my code:
print str[float[1/3]]+'%'
and it shows:
0.0%
but I want to get 33%
What can I do?
martineau
115k25 gold badges160 silver badges283 bronze badges
asked Mar 15, 2011 at 2:10
3
format
supports a percentage floating point precision type:
>>> print "{0:.0%}".format[1./3]
33%
If you don't want integer division, you can import Python3's division from __future__
:
>>> from __future__ import division
>>> 1 / 3
0.3333333333333333
# The above 33% example would could now be written without the explicit
# float conversion:
>>> print "{0:.0f}%".format[1/3 * 100]
33%
# Or even shorter using the format mini language:
>>> print "{:.0%}".format[1/3]
33%
maxymoo
33.5k9 gold badges89 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 .format[]
format method:
>>> '{:.1%}'.format[1/3.0]
'33.3%'
answered May 20, 2014 at 16:00
5
Just for the sake of completeness, since I noticed no one suggested this simple approach:
>>> print["%.0f%%" % [100 * 1.0/3]]
33%
Details:
%.0f
stands for "print a float with 0 decimal places", so%.2f
would print33.33
%%
prints a literal%
. A bit cleaner than your original+'%'
1.0
instead of1
takes care of coercing the division to float, so no more0.0
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
prob = 1.0/3.0
print[f"{prob:.0%}"]
answered Sep 24, 2019 at 21:48
menrfamenrfa
1,23711 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: //docs.python.org/library/string.html#format-specification-mini-language
To specify a percent conversion and precision.
>>> float[1] / float[3]
[Out] 0.33333333333333331
>>> 1.0/3.0
[Out] 0.33333333333333331
>>> '{0:.0%}'.format[1.0/3.0] # use string formatting to specify precision
[Out] '33%'
>>> '{percent:.2%}'.format[percent=1.0/3.0]
[Out] '33.33%'
A great gem!
answered Mar 15, 2011 at 2:16
Then you'd want to do this instead:
print str[int[1.0/3.0*100]]+'%'
The .0
denotes them as floats and int[]
rounds them to integers afterwards again.
answered Mar 15, 2011 at 2:16
3
I use this
ratio = round[1/3, 2]
print[f"{ratio} %"]
output: 0.33 %
answered Aug 19, 2021 at 8:16
DGKangDGKang
16212 bronze badges
1
this is what i did to get it working, worked like a charm
divideing = a / b
percentage = divideing * 100
print[str[float[percentage]]+"%"]
answered Jul 11 at 20:59