Python基础(更新中)

Python基础01 格式化输出1. 整数的输出符号说明%o八进制%d十进制%x十六进制print('%o' % 20) # output:24print('%d' % 20) # output:20print('%x' % 20) # output:142. 浮点数的输出符号说明备注%f保留小数点后6位有效数字%.3f:保留小数点后3位有效数字%e保留小数点后6位有效数字,以指数形式输出%.3e:保留小数
摘要由CSDN通过智能技术生成

01 格式化输出

1. 整数的输出
符号说明
%o八进制
%d十进制
%x十六进制
print('%o' % 20)  # output:24
print('%d' % 20)  # output:20
print('%x' % 20)  # output:14
2. 浮点数的输出
符号说明备注
%f保留小数点后6位有效数字%.3f:保留小数点后3位有效数字
%e保留小数点后6位有效数字,以指数形式输出%.3e:保留小数点后3位有效数字,使用科学计数法
print('%f' % 1.11)    # output: 1.110000
print('%.1f' % 1.11)  # output: 1.1
print('%e' % 1.11)    # output: 1.110000e+00
print('%.3e' % 1.11)  # output: 1.110e+00
3. 字符串的输出
符号备注
%s%10s:右对齐,占位符为10位;%-10s:左对齐,占位符为10位;%.2s:截取2位字符串
print('%10s' % 'hello')   # output:      hello
print('%-10s' % 'hello')  # output: hello
print('%.2s' % 'hello')   # output: he
print('%10.2s' % 'hello') # output:         he

02 取整

符号说明
math.ceil()向上取整
round()四舍五入当末尾的5的前一位为奇数时向绝对值更大的方向取整,当末尾的5的前一位为偶数时去尾取整
math.floor()向下取整
import math

print(math.ceil(1.55))   # output: 2
print(round(1.55))       # output: 2
print(math.floor(1.55))  # output: 1

03 正则表达式

Python正则表达式指南: https://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html
re模块官方文档: https://docs.python.org/zh-cn/3.8/library/re.html

常用字符:

字符描述
^匹配字符串的开头
$匹配字符串的末尾
*匹配前一个字符0次或无数次
+匹配前一个字符1次或无数次
匹配前一个字符0次或1次
\s匹配任意空白字符,等价于 [ \t\n\r\f]
\d匹配任意数字,等价于 [0-9]
\w匹配字母数字及下划线,等价于’[A-Za-z0-9_]’
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值