Python 字符串格式化输出方式
字符串格式化有两种方式:百分号方式、format方式。
其中,百分号方式比较老,而format方式是比较先进的,企图替代古老的方式,目前两者共存。
1、百分号方式
格式:%[(name)][flags][width].[precision]typecode
- (name) 可选,用于选择指定的key
- flags 可选,可供选择的值有:
- + 右对齐:正数的加正号,负数的加负号
- - 左对齐:正数前没有负号,负数前加负号
- width 可选,占有宽度
- .precision 可选,小数点后保留的位数
- typecode 必选
- s,获取传入的对象__str__方法的返回值,并将其格式化到指定位置
- r,获取传入对象的__repr__方法的返回值,并将其格式化到指定位置
- c,整数:将数字转换成其unicode对应的值,10进制范围为0 <= i <=1114111
- o,将整数转换成八进制表示,并将其格式化到指定位置
- x,将整数转换成16进制,并将其格式化到指定位置
- d,将整数,浮点数转化为十进制表示,并将其格式化到指定位置
例子:
>>> s = 'hello, %s!' % 'python'
>>> s
'hello, python!' >>> s = 'hello, %s, %d!' % ('python', 2018)
>>> s
'hello, python, 2018!' >>> s = 'hello, %(name)s, %(year)d!' % {'name': 'python', 'year': 2018}
>>> s
'hello, python, 2018!' >>> s = 'hello, %(name)+10s, %(year)-10d!' % {'name': 'python', 'year': 2018}
>>> s
'hello, python, 2018 !' >>> s = 'hello, %(name)s, %(year).3f!' % {'name': 'python', 'year': 2018}
>>> s
'hello, python, 2018.000!'
%r 与 %s 区别:
%r 用来做 debug 比较好,因为它会显示变量的原始数据(raw data),而其它的符号则是用来向用户显示输出的。
>>> a = 'sunday'
>>> print("Today is %s" % a)
Today is sunday
>>> print("Today is %r" % a)
Today is 'sunday' # 格式化部分用单引号输出 >>> from datetime import datetime
>>> d = datetime.now()
>>> print('%s' % d)
2018-09-10 08:52:00.769949
>>> print('%r' % d)
datetime.datetime(2018, 9, 10, 8, 52, 0, 769949) # 可以看见与上面输出存在明显的区别
2、format方式
>>> s = 'hello, {}, {}'.format('python', 2018)
>>> s
'hello, python, 2018' >>> s = 'hello, {0}, {1}, hi, {0}'.format('python', 2018)
>>> s
'hello, python, 2018, hi, python' >>> s = 'hello, {name}, {year}, hi, {name}'.format(name='python', year=2018)
>>> s
'hello, python, 2018, hi, python' >>> s = 'hello, {:s}, {:d}, hi, {:f}'.format('python', 2018, 9.7)
>>> s
'hello, python, 2018, hi, 9.700000'
Python 字符串格式化输出方式的更多相关文章
- python字符串格式化输出
python格式化输出 python格式化输出有两种方式:百分号和format format的功能要比百分号方式强大,其中format独有的可以自定义字符填充空白.字符串居中显示.转换二进制.整数自动 ...
- python 字符串格式化输出 %d,%s及 format函数
旧式格式化方式:%s,%d 1.顺序填入格式化内容 s = "hello %s, hello %d"%("world", 100) print(s) 结果: ' ...
- python字符串格式化输出 %和format举例
#!/usr/bin/env python # -*- coding: UTF-8 -*- #pyversion:python3.5 #owner:fuzj s1 = "i am %s, i ...
- python 字符串格式化 输出
1. 需要输出3列,为了输出好看,需要制定每一列的宽度: ‘%6.2f’ % 1.235 # 长度为6,保留2为小数 print '{0:20} {1:<20} {1:<20}\r\n'. ...
- python之格式化输出(3种方式)
python3.6后支持3种格式化输出方式,其中前两种为%-formatting及str.format ,第三种即为 f-string. 1.%-formatting 据传该格式化方法源于C.. &g ...
- (Python )格式化输出、文件操作、json
本节学习Python的格式化输出,文件操作以及json的简单用法 1.格式化输出 将非字符串类型转换成字符串,可以使用函数:str() 或者repr() ,(这两个函数的区别目前我还没搞懂,求解答) ...
- Python基本格式化输出
什么叫格式化输出? 数据按照某种特殊的要求输出 假如输入一个整数,希望整数按照十六进制,八进制输出,如果输入一个小数,希望小数保留后面2位数然后输出,或者以科学计数法的方式来输出小数.字符串的输出希望 ...
- python字符串格式化之学习笔记
在python中格式化输出字符串使用的是%运算符,通用的形式为 •格式标记字符串 % 要输出的值组其中,左边部分的”格式标记字符串“可以完全和c中的一致.右边的'值组'如果有两个及以上的值则需要用小括 ...
- Python学习:12.Python字符串格式化
字符串格式化 讲解Python这么久,也没有讲解Python的字符串的格式化,那我们今天就来了解一下python字符串格式化的强大之处. 首先我们先理解一下为什么要有字符串的格式化,就是为了方便字符串 ...
随机推荐
- Poj(2236),简单并查集
题目链接:http://poj.org/problem?id=2236 思路很简单,傻逼的我输出写成了FALL,然后遍历的时候for循环写错了,还好很快我就Debug出来了. #include < ...
- python剑指offer最小的K个数
题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 思路: 使用快排中的partition思想. ①我们设定part ...
- 自己编写shave函数
import numpy def shave(I,border=None): I = I[border[0]:I.shape[0]-border[0],border[1]:I.shape[1]-bor ...
- How good are detection proposals, really?
How good are detection proposals, really? J. Hosang, R. Benenson, B. Schiele Oral at BMVC 2014 http: ...
- Java框架 面试题总结
一. Struts1.x 4 1. struts1优缺点,为什么要使用struts1 4 2. Struts1核心组件 4 3. Strust1请求处理流程 工作原理 4 4. Struts1线程安 ...
- 【iOS】史上最全的iOS持续集成教程 (上)
:first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...
- Eclipse中文乱码解决方案
Eclipse中文乱码解决方案 1)第一个设置:window>perferences>general>workspace>text file encoding 2)Jsp编码问 ...
- 使用ansible安装配置zabbix客户端
ansible角色简介: 目录名 说明 defaults 默认变量存放目录 handlers 处理程序(当发生改变时需要执行的操作) meta 角色依赖关系处理 tasks 具体执行的任务操作定义 t ...
- 基于WSAAsyncSelect模型的两台计算机之间的通信
任务目标 编写Win32程序模拟实现基于WSAAsyncSelect模型的两台计算机之间的通信,要求编程实现服务器端与客户端之间双向数据传递.客户端向服务器端发送"请输出从1到1000内所有 ...
- 【Effective C++ 读书笔记】导读 Introduction
学习程序语言根本大法是一回事,学习如何以某种语言设计并实现高效程序则是另一回事. 一组明智选择并精心设计的classes.functions.templates可使程序编写容易.直观.高效.并且远离错 ...