python之格式化输出
字符串格式化有两种方式,%和format
先介绍下%号的方法
- #%s的语法结构,叫做占位符,就是先占一个位置,然后我们用真实的要显示的数据替换占位符即可
#最简单的用法就是下面的方式,其实%s还有其他的功能
# s = '我的名字是%s,我的年龄是%d' %('alex',2)
# print(s)- # 我的名字是alex,我的年龄是2
- #%[(name)][flags][width].[precision]typecode
#第一个参数[(name)],默认情况下%是按照顺序进行格式化的,用了name,我们可以用name来进行字符串格式化,比如下面的例子,
#可以根据名字来做格式化
# s = '我的名字是%(name)s,我的年龄是%(age)d' %{'name':'alex','age':12}
# print(s)- # 我的名字是alex,我的年龄是12
- #flags,可以是 + - 空格 0,这个一般和width配合起来使用,%没有居中的功能
#+号,右对齐,正数前加+号,负数前加-号
# s = 'my age is %(age1)+10dDDDD,my brother age is%(age2)+10d' %{'age1':12,'age2':-12}
# print(s)
# my age is +12DDDD,my brother age is -12
#-号,左对齐,正数前无符号,负数前加-号
# s = 'my age is %(age1)-10dDDDD,my brother age is%(age2)-10d' %{'age1':12,'age2':-12}
# print(s)
# my age is 12 DDDD,my brother age is-12
#0 ,右对齐,正数前无符号,负数前加-号,用0填补空白处
#空格,右对齐,正数前加空格,负数前加-号- #width,占的位置的长度
- #precision 小数点后显示多少位,这里会做四舍五入
# s = 'my age is %(age1)-10fDDDD,my brother age is%(age2)-10d' %{'age1':12.1234445,'age2':-12}
# print(s)- # my age is 12.123444 DDDD,my brother age is-12
- # s = 'my age is %(age1)-10.4fDDDD,my brother age is%(age2)-10d' %{'age1':12.1234445,'age2':-12}
# print(s)- # my age is 12.1234 DDDD,my brother age is-12
- # typecode
#s:是字符串
#o:将传入的值转换为八进制,然后把转换后的值做字符串格式化
#x:将传入的值转换为16进制,然后在把转换后的值做字符串格式化
#d,正数
#f,浮点数
# s = 'aaaaaadd %d------------%o----------%x' %(15,15,15)
# print(s)
# aaaaaadd 15------------17----------f- #e|E科学计数法,一个是显示小写的e,一个是大写的E
# s = '%e--------------%E' %(100000000000000000000,100000000000000000)
# print(s)
# 1.000000e+20--------------1.000000E+17- #%
# s = 'football %'
# print(s)
# s = 'football %%'
# print(s)- # football %
# football %%- # s = 'name is %s,%' %('dddd')
# print(s)- #这样会报错的
# s = 'name is %s,%%' %('dddd')
# print(s)- # name is dddd,%
- 下面介绍下format的方法
- # [(fill)align][sign][#][0][width][,][.precision][type]
- #fill:【可选】空白处填充的字符,这里只能写一个字符
- #align,对齐方式,需要配合width使用
#<:内容右对齐
#>:内容左对齐
#=:
#^:内容居中- #sign 【可选】有无符合数字、
# + 正号加正,负数加负
#- 正号不变,负数加负
#空格,正号加空格,负数加负- # #号,【可选】对于二进制,八进制,16进制,如果加上#号,会显示0b 0o 0x,如果不加,则不显示前面的符号
- #,号,如果比较大数字,可以每三位加一个逗号100,000,,000
- #width,格式化位所占的宽度
- #.precision,小数点的保留位
- #type,格式化的类型
- #b,将传入的数字转换成二进制
- #format的常用的用法,这个是按照顺序来格式化的
# s = 'aaaaaaaadd{0},dddd{0}----{1}' .format('AAA','BBBB')
# print(s)- # aaaaaaaaddAAA,ddddAAA----BBBB
- #按照名字做字符串格式化
# s = '-------{name:s},-------{age:d}------{name:s}' .format(name = 'AAA',age = 23)
# print(s)
# -------AAA,-------23------AAA- #[(fill)align][sign][#][0][width][,][.precision][type],这些全部都要放在冒号后面,冒号前面只能有一个字段,就是name
- # s = '-------------{:@^10s}-----------' .format('AAA')
# print(s)- # -------------@@@AAA@@@@-----------
- # s = '-------------{name:@^10s}-----------' .format(name = 'AAA')
# print(s)- # -------------@@@AAA@@@@-----------
- # s = '--------{:%}' .format(1.345566677)
# print(s)- # --------134.556668%
- #format必须要掌握的几种方式
# s = 'i am {},age {},{}' .format('foot',12,'boy')
# print(s)- # i am foot,age 12,boy
- # s = 'i am {},age {},{}' .format(*['foot',12,'boy'])
# print(s)- # i am foot,age 12,boy
- # s = 'i am {0},age {1},{1}' .format('foot',12)
# print(s)- # i am foot,age 12,12
- # s = 'i am {0},age {1},{1}' .format(*['foot',12])
# print(s)- # i am foot,age 12,12
- # s = 'i am {name},age is {age}' .format(name = 'foot',age = 12)
# print(s)- # i am foot,age is 12
- # s = 'i am {name},age is {age}' .format(**{'name':'football','age':12})
# print(s)- # i am football,age is 12
- # s = 'i am {name:s},age is {age:d}' .format(name = 'foot',age = 12)
# print(s)- # i am foot,age is 12
- # s = '--{:b}--{:d}--{:x}--{:X}--{:%}' .format(15,15,15,15,15)
# print(s)- # --1111--15--f--F--1500.000000%
- # s = '--{0:b}--{0:d}--{0:x}--{0:X}--{0:%}' .format(15)
# print(s)- # --1111--15--f--F--1500.000000%
- s = '--{num:b}--{num:d}--{num:x}--{num:X}--{num:%}' .format(num = 15)
print(s)- # --1111--15--f--F--1500.000000%
python之格式化输出的更多相关文章
- (Python )格式化输出、文件操作、json
本节学习Python的格式化输出,文件操作以及json的简单用法 1.格式化输出 将非字符串类型转换成字符串,可以使用函数:str() 或者repr() ,(这两个函数的区别目前我还没搞懂,求解答) ...
- python的格式化输出
Python的格式化输出有两种: 一.类似于C语言的printf的方法 二.类似于C#的方法
- python print格式化输出。
python print格式化输出. 1. 打印字符串 print ("His name is %s"%("Aviad")) 效果: 2.打印整数 print ...
- Python 基础 格式化输出
Python 基础 格式化输出 现在需要我们录入我们身边好友的信息,格式如下: ------------ info of Alex Li ---------- Name : Alex Li Age : ...
- python字符串格式化输出
python格式化输出 python格式化输出有两种方式:百分号和format format的功能要比百分号方式强大,其中format独有的可以自定义字符填充空白.字符串居中显示.转换二进制.整数自动 ...
- Python print格式化输出
python中的print格式化输出,基本格式:"[字符串]%格式1[字符串]%格式2[字符串]....."%(string1,string2.....) 格式符号 ------- ...
- Python之格式化输出讲解
1.格式化输出整数python print也支持参数格式化,与C言的printf似, strHello = "the length of (%s) is %d" %(Hello W ...
- Python基本格式化输出
什么叫格式化输出? 数据按照某种特殊的要求输出 假如输入一个整数,希望整数按照十六进制,八进制输出,如果输入一个小数,希望小数保留后面2位数然后输出,或者以科学计数法的方式来输出小数.字符串的输出希望 ...
- Python之格式化输出,初始编码以及运算符
一.题型 1.使用while循环输入 1 2 3 4 5 6 8 9 10 count = 0 while count < 10: count += 1 #count = count + ...
- python 浅析格式化输出和深浅copy
一,格式化输出 今天主要想记录一下关于格式化输出的例子,然后结合了自己的理解,分析如下: 格式是 :百分号+占位符 主要有三种使用形式:%s (其中s表示string)表示字符串 %d (其中d表 ...
随机推荐
- for循环实例2
九九乘法表: //九九乘法表 ; x < ; x++) { ; y <=x;y++ ) { Console.Write(y.ToString()+"*"+x.ToStr ...
- shell脚本运行springboot项目jar包
#!/bin/bash APP_NAME=AutomationGuide-0.0.1-SNAPSHOT.jar #使用说明,用来提示输入参数 usage() { echo "please e ...
- iframe+form上传文件
<iframe id="iframe1" name="ifra1" style="display: none"></ifr ...
- lientDataset的Delta与XML相互转换
一个ClientDataset的Delta与XML相互转换的文章:大家都知道TClientDataSet的Delta属性保存数据集的变化,但是Delta是OleVariant类型的属性,这样如果用De ...
- 批量杀死多个进程 linux kill
批量杀进程 -| “grep -v grep”是在列出的进程中去除含有关键字“grep”的进程. “cut -c 9-15”是截取输入行的第9个字符到第15个字符,而这正好是进程号PID,也有使用aw ...
- CSS GRID ESSENTIALS
CSS GRID ESSENTIALS Review At this point, we've covered a great deal of different ways to manipulate ...
- SQL Server 用角色(Role)管理数据库权限
当数据库越来越多,连接到数据库的应用程序,服务器,账号越来越多的时候,为了既能达到满足账号操作数据权限需求,又不扩大其操作权限,保证数据库的安全性,有时候需要用角色来参与到权限管理中,通过角色做一个权 ...
- C++ CBitmap,HBitmap,Bitmap区别及联系
加载一位图,可以使用LoadImage: HANDLE LoadImage(HINSTANCE hinst,LPCTSTR lpszName,UINT uType,int cxDesired,int ...
- asp.net 服务器 上传文件到 FTP服务器
private string ftpServerIP = "服务器ip";//服务器ip private string ftpUserID = "ftp的用户名" ...
- Eclipse绿豆沙护眼
Window-->Preferences-->Editors——>Text Editors —— Background color 背景颜色向你推荐:色调:85.饱和度:123.亮度 ...