程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python placeholder %s%d%f

編輯:Python

% placeholder is a placeholder that is often used in python programs.
Its role is to occupy a position for the following variable value
For example, to format the output of the nine-nine multiplication table, the % placeholder is used

# Ninety-nine multiplication tableprint('Nine-Nine Multiplication Table')for i in range(1, 11):for j in range(1, i +1):print('%dx%d=%d\t'span> % (j, i, j * i), end="")print("")

Result:

%d integer used above(int) placeholder

% placeholders commonly used in python programs

1, %s, string placeholder, use str() method to convert any Python object, including integer int, floating point float, such as:

print('%s,%s,%s'%('string',10,1.0))

Result:
2, %d, integer (int)A placeholder, which can also represent a floating-point number float (only the integer part is taken), such as:

print('represents an integer: %d, represents a floating point number: %d'%(1,1.9))

Result:
3, %f, float (float) placeholder, which can also represent an integer (int), which are reserved for 6 decimal places by default, such as:

print('represents a floating point number: %f, represents an integer: %f'%(1.234,1))

Result:
Problem: If we don't want to keep 6 decimalsBit, I want to define how many bits to keep, how to write it?
Solution: add the number of digits to be reserved after %
Format: %.nf, n means to retain n decimal places

print('Keep 2 decimal places: %.2f'%1.2345)

Result:


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved