一、%格式化输出

1、整数的输出(参照ASCII)

  • %o —— oct 八进制
  • %d —— dec 十进制(digit )
  • %x —— hex 十六进制
>>> print('%o' % 20)
24
>>> print('%d' % 20)
20
>>> print('%x' % 20)
14

2、浮点数输出

  • %f —— float 保留小数点后面六位有效数字

    • %.3f,保留3位小数位
  • %e —— 保留小数点后面六位有效数字,指数形式输出
    • %.3e,保留3位小数位,使用科学计数法
  • %g —— 在保证六位有效数字的前提下,使用小数方式,否则使用科学计数法
    • %.3g,保留3位有效数字,使用小数或科学计数法
>>> print('%f' % 1.11)  # 默认保留6位小数
1.110000
>>> print('%.1f' % 1.11) # 取1位小数
1.1
>>> print('%e' % 1.11) # 默认6位小数,用科学计数法
1.110000e+00
>>> print('%.3e' % 1.11) # 取3位小数,用科学计数法
1.110e+00
>>> print('%g' % 1111.1111) # 默认6位有效数字
1111.11
>>> print('%.7g' % 1111.1111) # 取7位有效数字
1111.111
>>> print('%.2g' % 1111.1111) # 取2位有效数字,自动转换为科学计数法
1.1e+03

3、字符串输出

  • %s —— string 字符串
  • %10s —— 右对齐,占位符10位
  • %-10s —— 左对齐,占位符10位
  • %.2s —— 截取2位字符串
  • %10.2s —— 10位占位符,截取两位字符串
>>> print('%s' % 'hello world')  # 字符串输出
hello world
>>> print('%20s' % 'hello world') # 右对齐,取20位,不够则补位
hello world
>>> print('%-20s' % 'hello world') # 左对齐,取20位,不够则补位
hello world
>>> print('%.2s' % 'hello world') # 取2位
he
>>> print('%10.2s' % 'hello world') # 右对齐,取2位
he
>>> print('%-10.2s' % 'hello world') # 左对齐,取2位
he
name = input("Name:")
age = int(input("Age:"))
job = input("Job:")
salary = int(input("Salary:")) msg = """
------------info of %s------------
Name:%s
Age:%d
Job:%s
Salary:%d
----------------end---------------
"""%(name,name,age,job,salary) print(msg)

二、format 格式化

1.format()

  • str.format() 该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号 "{ }" 作为特殊字符代替 "%"
  • { } 中不设参数
>>>"{} {}".format("hello", "world")    # 不设置指定位置,按默认顺序
'hello world' >>> "{0} {1}".format("hello", "world") # 设置指定位置
'hello world' >>> "{1} {0} {1}".format("hello", "world") # 设置指定位置
'world hello world'
  • { } 中设置参数
# 通过变量设置参数
print("My name is {name},and I am {age} years old!".format(name = "zhangsan",age = "")) # 通过字典设置参数
info = {"name": "zhangsan", "age": ""}
print("My name is {name},and I am {age} years old!".format(**info)) # 通过列表索引设置参数
msg = ["zhangsan",""]
print("My name is {0[0]},and I am {0[1]} years old!".format(msg)) ---> My name is zhangsan,and I am 25 years old!
---> My name is zhangsan,and I am 25 years old!
---> My name is zhangsan,and I am 25 years old!
  • str.format() 格式化数字
    • ^, <, > 分别是居中、左对齐、右对齐,后面带宽度, : 号后面带填充的字符,只能是一个字符,不指定则默认是用空格填充。
    • + 表示在正数前显示 +,负数前显示 -;  (空格)表示在正数前加空格
>>> print("{:.2f}".format(3.1415926));
3.14
数字 格式 输出 描述
3.1415926 {:.2f} 3.14 保留小数点后两位
3.1415926 {:+.2f} +3.14 带符号保留小数点后两位
-1 {:+.2f} -1.00 带符号保留小数点后两位
2.71828 {:.0f} 3 不带小数
5 {:0>2d} 05 数字补零 (填充左边, 宽度为2)
5 {:x<4d} 5xxx 数字补x (填充右边, 宽度为4)
10 {:x<4d} 10xx 数字补x (填充右边, 宽度为4)
1000000 {:,} 1,000,000 以逗号分隔的数字格式
0.25 {:.2%} 25.00% 百分比格式
1000000000 {:.2e} 1.00e+09 指数记法
13 {:>10d}         13 右对齐 (默认, 宽度为10)
13 {:<10d} 13 左对齐 (宽度为10)
13 {:^10d}     13 中间对齐 (宽度为10)
  • 注意:可以使用大括号 {} 来转义大括号
print ("{}今年{{25}}岁了".format("张三"))

2.format_map()

  • 参数为字典
print("I am {name},and I am {age} years old!".format_map({"name":"zhangsan","age":25}))

---> I am zhangsan,and I am 25 years old!

python(格式化输出)的更多相关文章

  1. python格式化输出【转】

    今天写代码时,需要统一化输出格式进行,一时想不起具体细节,用了最笨的方法,现在讲常见的方法进行一个总结. 一.格式化输出 1.整数的输出 直接使用'%d'代替可输入十进制数字: >>> ...

  2. Python格式化输出的三种方式

    Python格式化输出的三种方式 一.占位符 程序中经常会有这样场景:要求用户输入信息,然后打印成固定的格式比如要求用户输入用户名和年龄,然后打印如下格式:My name is xxx,my age ...

  3. python格式化输出及大量案例

    python格式化输出符号及大量案例 1.格式化输出符号 python格式化输出符号 格式化符号 含义 %c 转化成字符 %r 优先使用repr()函数进行字符串转化 %s 转换成字符串,优先使用st ...

  4. Python 格式化输出

    转载 今天写程序又记不清格式化输出细节了--= =索性整理一下. 注意: 与C/C++  不同的是这里括号后面不需要加' , '号. python print格式化输出. 1. 打印字符串 print ...

  5. Python格式化输出

    今天写程序又记不清格式化输出细节了……= =索性整理一下. python print格式化输出. 1. 打印字符串 print ("His name is %s"%("A ...

  6. [No000063]Python格式化输出

    python print格式化输出. 1. 打印字符串 print ("His name is %s"%("Aviad")) 效果: 2.打印整数 print ...

  7. [转]Python格式化输出

    今天写程序又记不清格式化输出细节了……= =索性整理一下. python print格式化输出. 1. 打印字符串 print ("His name is %s"%("A ...

  8. Python学习教程(learning Python)--1.2.2 Python格式化输出基础

    本节讨论为何要格式化输出数据? 先看一段代码吧,本程序的功能是计算月支付金额. amount_due = 5000.0 #年支付金额 monthly_payment = amount_due / 12 ...

  9. Python格式化输出%s和%d

    python print格式化输出. 1. 打印字符串 print ("His name is %s"%("Aviad")) 效果: 2.打印整数 print ...

  10. Python 格式化输出 —— 小数转化为百分数

    比如将 0.1234 转化为 12.34% 的形式: rate = .1234 print('%.2f%%' % (rate * 100)) 第一个百分号和 .2f 相连,表示浮点数类型保留小数点后两 ...

随机推荐

  1. webpack配置示例

    var webpack = require('webpack'); var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('commo ...

  2. java 代码执行cmd 返回值异常 (关于JAVA Project.waitfor()返回值是1)

    关于JAVA Project.waitfor()返回值是1   0条评论 Project.waitfor()返回值是1,找了很久从网上没有发现关于1的说明. 这时对源代码调试了一下,发现Project ...

  3. day22作业

    # 1.检索文件夹大小的程序,要求执行方式如下 # python3.8 run.py 文件夹 import os,sys l = sys.argv[1] size = 0 def get_size(f ...

  4. Spring IoC getBean 方法详解

    前言 本篇文章主要介绍 Spring IoC 容器 getBean() 方法. 下图是一个大致的流程图: 正文 首先定义一个简单的 POJO,如下: public class User { priva ...

  5. 归并排序(归并排序求逆序对数)--16--归并排序--Leetcode面试题51.数组中的逆序对

    面试题51. 数组中的逆序对 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 示例 1: 输入: [7,5,6,4] 输出 ...

  6. C#多线程(12):线程池

    目录 线程池 ThreadPool 常用属性和方法 线程池说明和示例 线程池线程数 线程池线程数说明 不支持的线程池异步委托 任务取消功能 计时器 线程池 线程池全称为托管线程池,线程池受 .NET ...

  7. 15分钟从零开始搭建支持10w+用户的生产环境(三)

    上一篇文章介绍了这个架构中,选择MongoDB做为数据库的原因,及相关的安装操作. 原文地址:15分钟从零开始搭建支持10w+用户的生产环境(二)   三.WebServer 在SOA和gRPC大行其 ...

  8. vue显示富文本

    来源:https://segmentfault.com/q/1010000013952512 用  v-html 属性解决

  9. JS静态变量和静态函数

    本文链接:https://blog.csdn.net/u012790503/article/details/46278521 function A(){this.id = "我是AA&quo ...

  10. 给动态ajax添加的元素添加click事件

    $(document).on('click','div',function(){alert(1)}); .live()方法也是可以的