Gunakan penentu format dengan f strings ( selengkapnya di sini ).
- Anda dapat mengontrol jumlah desimal :
pi = 3.141592653589793238462643383279
print(f'The first 6 decimals of pi are {pi:.6f}.')
The first 6 decimals of pi are 3.141593.
- Anda dapat mengonversi ke persentase :
grade = 29/45
print(f'My grade rounded to 3 decimals is {grade:.3%}.')
My grade rounded to 3 decimals is 64.444%.
- Anda dapat melakukan hal lain seperti mencetak panjang konstan :
from random import randint
for i in range(5):
print(f'My money is {randint(0, 150):>3}$')
My money is 126$
My money is 7$
My money is 136$
My money is 15$
My money is 88$
- Atau bahkan mencetak dengan pemisah ribuan koma :
print(f'I am worth {10000000000:,}$')
I am worth 10,000,000,000$