编程语言
首页 > 编程语言> > Python入门零散知识整理(! 格式化输出)

Python入门零散知识整理(! 格式化输出)

作者:互联网

目录

内置类型转换

type()可直接查看变量类型

浮点数

补充:

>>>dict(name = 'aloha', food = 'apple pie', id = '0')
{'name': 'aloha', 'food': 'apple pie', 'id': '0'}

数学

字符串

变量赋值模型(有书上称为便签模型)

python中赋值是创建一个引用的过程(在Python中,从变量到对象的连接称作引用)。变量实际上是指向对象的一个指针。可以用变量指向(也许是引用)任何类型的数据。

根据右值可将赋值分为两种

  1. 右值为具体的值
    用该值在内存中创建一个对象,并使该变量引用此对象。
    注意:数字和字符串在Python中其实均不可改变。
  2. 右值为变量
    共享同一个对象。

注:引用可以通过del删除。



Python中的列表可以起到类似指针的效果。

a = [1, 2, 3]
b = a
a[0] = '1'
print(b)
['1', 2, 3]

在修改a[0]后,b[0]也被修改了。
a[0] = '1'修改的是内存中列表的第一个元素。ab的值都没变,但内存中列表的值发生了改变。

简单输入和输出

格式化输入输出

1. 字符串格式化运算符% (类似C中的printf()函数)

  1. 在%操作符的左侧放置一个需要进行格式化的字符串,这个字符串带有一个或多个嵌入的转换目标,都以%开头(例如,%d)。
  2. 在%操作符右侧放置一个(或多个,嵌入到元组中)对象,这些对象将会插入到左侧想让Python进行格式化字符串的一个(或多个)转换目标的位置上去。

标准格式:

%[(name)][flags][minimumwidth][.precision]typecode
  1. 字符串格式化代码 (typecode, 以下称conversion) :
    | Conversion | Meaning | Notes |
    | --- | --- | --- |
    | 'd' | Signed integer decimal. | |
    | 'i' | Signed integer decimal. | |
    | 'o' | Signed octal value. | (1) |
    | 'u' | Obsolete type – it is identical to 'd'. | (6) |
    | 'x' | Signed hexadecimal (lowercase). | (2) |
    | 'X' | Signed hexadecimal (uppercase). | (2) |
    | 'e' | Floating point exponential format (lowercase). | (3) |
    | 'E' | Floating point exponential format (uppercase). | (3) |
    | 'f' | Floating point decimal format. | (3) |
    | 'F' | Floating point decimal format. | (3) |
    | 'g' | Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. | (4) |
    | 'G' | Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. | (4) |
    | 'c' | Single character (accepts integer or single character string). | |
    | 'r' | String (converts any Python object using repr()). | (5) |
    | 's' | String (converts any Python object using str()). | (5) |
    | 'a' | String (converts any Python object using ascii()). | (5) |
    | '%' | No argument is converted, results in a '%' character in the result. | |

Notes:

  1. The alternate form causes a leading octal specifier ('0o') to be inserted before the first digit.
  2. The alternate form causes a leading '0x' or '0X' (depending on whether the 'x' or 'X' format was used) to be inserted before the first digit.
  3. The alternate form causes the result to always contain a decimal point, even if no digits follow it.
    The precision determines the number of digits after the decimal point and defaults to 6.
  4. The alternate form causes the result to always contain a decimal point, and trailing zeroes are not removed as they would otherwise be.
    The precision determines the number of significant digits before and after the decimal point and defaults to 6.
  5. If precision is N, the output is truncated to N characters.
  6. See PEP 237.
  1. flags
    python i = 4.53459 print(r'%-10.5f: ', '...%-10.5f...'%(i)) # - 左对齐 print(r'%+10.5f: ', '...%+10.5f...'%(i)) # + 显示符号 print(r'%010.5f: ', '...%010.5f...'%(i)) # 0 空位补零
    %-10.5f: ...4.53459 ... %+10.5f: ... +4.53459... %010.5f: ...0004.53459...

  2. minimumwidth和precision
    • minimumwidth指定至少的数字长度(包括小数点、符号),precision指定数字精度(即小数点后位数)
    • 可以用*表明两者的值从%运算符后获取
    • 精度默认为0
    • 直接用%f,则默认保留6位小数点后数字
i = 4.53459
print(r'%-5.2f : ', '...%-5.2f...'%(i))
print(r'%-f    : ', '...%-f...'%(i))
print(r'%-.f   : ', '...%-.f...'%(i))
print(r'%-0.0f : ', '...%-0.0f...'%(i))

width = 5
print(r'%-*.*f : ', '...%-*.*f...'%(width, width-2, i))
%-5.2f :  ...4.53 ...
%-5.10f:  ...4.5345900000...
%-f    :  ...4.534590...
%-.f   :  ...5...
%-0.0f :  ...5...
%-*.*f :  ...4.535...
  1. name

用在基于字典的字符串格式化中,如:

>>>"%(name)s-%(id)d loves %(food)s"%{**dict(name = 'aloha', food = 'apple pie', id = 0)}
'aloha-0 loves apple pie'

常搭配vars()使用:

>>>name, food = "John", "apple pie"
>>>print("I'm %(name)s. My favorite food is %(food)s"%vars())
I'm John. My favorite food is apple pie

2. 字符串格式化方法format()

函数

注意:在函数声明中,普通参数(regular parameter)必须在这两种参数前,*parameter必须在**parameter前。

标签:...,零散,格式化,name,format,Python,number,字符串,print
来源: https://www.cnblogs.com/alohana/p/12238501.html