Python3 字符串格式化

字符串的格式化方法分为两种,分别为占位符(%)和format方式。占位符方式在Python2.x中用的比较广泛,随着Python3.x的使用越来越广,format方式使用的更加广泛。

一 占位符(%)

%% 百分号标记

%c 字符及其ASCII码

%s 字符串

%d 有符号整数(十进制)

%u 无符号整数(十进制)

%o 无符号整数(八进制)

%x 无符号整数(十六进制)

%X 无符号整数(十六进制大写字符)

%e 浮点数字(科学计数法)

%E 浮点数字(科学计数法,用E代替e)

%f 浮点数字(用小数点符号)

%g 浮点数字(根据值的大小采用%e或%f)

%G 浮点数字(类似于%g)

%p 指针(用十六进制打印值的内存地址)

%n 存储输出字符的数量放进参数列表的下一个变量中

%d

实例(Python3.0+):

age = 29
print("my age is %d" %age)
#my age is 29

%s

实例(Python3.0+):

name = "makes"
print("my name is %s" %name)
#my name is makes

%f

实例(Python3.0+):

print("%6.3f" % 2.3)
#2.300
print("%f" %2.3)
#2.300000

%%

实例(Python3.0+):

tp1="percent %.2f %%" % 89.1234566
print (tp1) #percent 89.12 %

%()type

实例(Python3.0+):

info="I am %(name)s , %(age)d years old ." %{"name":"Lucy","age":8}
print(info) #I am Lucy , 8 years old

字符串格式控制%[(name)][flag][width][.][precision]type

name:可为空,数字(占位),命名(传递参数名,不能以数字开头)以字典格式映射格式化,其为键名

flag:标记格式限定符号,包含+-#0,+表示右对齐(会显示正负号),-左对齐,前面默认为填充空格(即默认右对齐),0表示填充0,#表示八进制时前面补充0,16进制数填充0x,二进制填充0b

width:宽度(最短长度,包含小数点,小于width时会填充)

precision:小数点后的位数,与C相同

type:输入格式类型,请看上面

info="I am %(name)-10s , %(age)d years old ." %{"name":"Lucy","age":8}
print(info) #I am Lucy , 8 years old .

补充:

user='root'
uid=0
gid=0
print(user,uid,gid,sep=":" ) #root:0:0

  

二 format方法

位置映射

实例(Python3.0+):

print("{}:{}".format('192.168.0.100',8888))
#192.168.0.100:8888

  

关键字映射

实例(Python3.0+):

print("{server}{1}:{0}".format(8888,'192.168.1.100',server='Web Server Info :'))
#Web Server Info :192.168.1.100:8888  

元素访问

实例(Python3.0+):

print("{0[0]}.{0[1]}".format(('baidu','com')))
#baidu.com 

  

填充对齐

  1. ^、<、>分别是居中、左对齐、右对齐

实例1(Python3.0+)

print("{0}*{1}={2:0>2}".format(3,2,2*3))

#3*2=06

print("{:*^30}".format('centered'))

#***********centered*********** 

 

实例2(Python3.0+):九九乘法表

for i in range(1,10):
a = 1
while a <= i:
print("{0}*{1}={2:0>2}".format(a,i,a*i),end="\t")
a +=1
print() """
1*1=01
1*2=02 2*2=04
1*3=03 2*3=06 3*3=09
1*4=04 2*4=08 3*4=12 4*4=16
1*5=05 2*5=10 3*5=15 4*5=20 5*5=25
1*6=06 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=07 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=08 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=09 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
"""

精度设置

实例(Python3.0+):

print("{:.3f}".format(2.1415))
#2.142 print("{:.10f}".format(3.1415))
#3.1415000000

  

更多使用:

print("{:,}".format(123456))#输出1234,56

print("{a:w^8}".format(a="8"))#输出www8wwww,填充w

print("%.5f" %5)#输出5.000000

print("%-7s3" %("python"))#输出python 3

print("%.3e" %2016)#输出2.016e+03,也可以写大E

print("%d %s" %(123456,"myblog"))#输出123456 myblog

print("%(what)s is %(year)d" % {"what":"this year","year":2016})#输出this year is 2016

print("{0}{1}".format("hello","fun"))#输出hellofun,这与CSharp的格式化字符(占位符)相似

print("{}{}{}".format("spkk",".","cn"))#输出spkk.cn

print("{a[0]}{a[1]}{a[2]}".format(a=["spkk",".","cn"]))#输出spkk.cn

print("{dict[host]}{dict[dot]}{dict[domain]}".format(dict={"host":"www","domain":"spkk.cn","dot":"."}))#输出www.spkk.cn

print("{a}{b}".format(a="python",b="3"))#输出python3

print("{who} {doing} {0}".format("python",doing="like",who="I"))#输出I like python

print ('number:{1:d}:{2:.2f}:{3:e}:{4:%}'.format(*[1,2,3000,4,5,])) #输出 number:2:3000.00:4.000000e+00:500.000000%

print ('number:{num:o}:{num:0>.2f}:{num:b}:{num:x}:{num:X}'.format(num=15)) #输出 number:17:15.00:1111:f:F

  

 1 print("{:,}".format(123456))#输出1234,56
2
3 print("{a:w^8}".format(a="8"))#输出www8wwww,填充w
4
5 print("%.5f" %5)#输出5.000000
6
7 print("%-7s3" %("python"))#输出python 3
8
9 print("%.3e" %2016)#输出2.016e+03,也可以写大E
10
11 print("%d %s" %(123456,"myblog"))#输出123456 myblog
12
13 print("%(what)s is %(year)d" % {"what":"this year","year":2016})#输出this year is 2016
14
15 print("{0}{1}".format("hello","fun"))#输出hellofun,这与CSharp的格式化字符(占位符)相似
16
17 print("{}{}{}".format("spkk",".","cn"))#输出spkk.cn
18
19 print("{a[0]}{a[1]}{a[2]}".format(a=["spkk",".","cn"]))#输出spkk.cn
20
21 print("{dict[host]}{dict[dot]}{dict[domain]}".format(dict={"host":"www","domain":"spkk.cn","dot":"."}))#输出www.spkk.cn
22
23 print("{a}{b}".format(a="python",b="3"))#输出python3
24
25 print("{who} {doing} {0}".format("python",doing="like",who="I"))#输出I like python

  

 

 

(十四)Python3 字符串格式化的更多相关文章

  1. python学习(二十四) 字符串格式化

    1: Test 1 a = 'city' b = 'country' print(" aaa %s bbb %s " % (a, b)) result: aaa city bbb ...

  2. Java开发笔记(三十五)字符串格式化

    前面介绍了字符串变量的四种赋值方式,对于简单的赋值来说完全够用了,即便是两个字符串拼接,也只需通过加号把两个目标串连起来即可.但对于复杂的赋值来说就麻烦了,假设现在需要拼接一个很长的字符串,字符串内部 ...

  3. Python3 字符串格式化

    1.字符串的格式化: 按照统一的规格去输出成为一个新的字符串 2.字符串格式化的方法: 1)format方法 fomat()有两个参数位置参数和关键字参数用中括号括起来{ } #{0}{1}为位置参数 ...

  4. Java开发笔记(三十四)字符串的赋值及类型转换

    不管是基本的char字符型,还是包装字符类型Character,它们的每个变量只能存放一个字符,无法满足对一串字符的加工.为了能够直接操作一连串的字符,Java设计了专门的字符串类型String,该类 ...

  5. python3字符串格式化format()函数的简单用法

    format()函数 """ 测试 format()函数 """ def testFormat(): # format()函数中有几个元素, ...

  6. C语言从零开始(十四)-字符串处理

    在软件开发过程中,字符串的操作相当频繁.在标准C语言库中提供了很多字符串处理的函数.今天我们来介绍一些常用的字符串处理函数.1. 字符串输入输出1.1 printf() scanf() 之前我们学习过 ...

  7. Python3 字符串格式化(%操作符)

    格式符 格式符为真实值预留位置,并控制显示的格式.格式符可以包含有一个类型码,用以控制显示的类型,如下: %s    字符串 (采用str()的显示) %r    字符串 (采用repr()的显示) ...

  8. Linux学习(十四)磁盘格式化、磁盘挂载、手动增加swap空间

    一.磁盘格式化 分好去的磁盘需要格式化之后才可以使用.磁盘分区一般用mke2fs命令或者mkfs.filesystemtype.这个filesystemtype分为ext4,ext3,xfs等等.xf ...

  9. LeetCode第十四题-字符串数组中最长的共同前缀

    Longest Common Prefix 问题简介: 编写一个函数来查找字符串数组中最长的公共前缀字符串,如果没有公共前缀,则返回空字符串"" 举例: 1: 输入: [“xwq” ...

随机推荐

  1. bzoj 3122: [Sdoi2013]随机数生成器【BSGS】

    题目要求的是: \[ ...a(a(a(ax+b)+b)+b)+b...=a^nx+a^{n-1}b+a^{n-2}b+...+b\equiv t(mod\ p) \] 后面这一大坨看着不舒服,所以考 ...

  2. springboot(七) 配置嵌入式Servlet容器

    github代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service ...

  3. 【SQL】从待选项中随机选一个

    由于SQL Server没有数组类型,所以在面对“从若干待选项中选一个”这种需求时,往往要采取变通办法,比如弄个‘a|b|c’这样的字符串然后对字符串进行处理:又或者把待选项塞进一个临时表,然后把问题 ...

  4. 安装使用electron辛路历程

    安装使用electron辛路历程 成功安装electron以及成功使用第一个应用,整整花费了我一整天的时间,各种百度,各种尝试.最终,终于总结了一个亲测可行的终极可执行方案: electron 简单介 ...

  5. [BZOJ4043/CERC2014]Vocabulary

    Description 给你三个字符串,这些字符串有些单词模糊不可认了,用"?"来代表. 现在你可以用任意英文小写字母来代表它们.要求是使得给定的三个字符串中 所有的"? ...

  6. Java Annontation(注解)详解

    java中经常用到注解(Annontation),索性整理了下关于注解的相关知识点: 一.概念 Annontation是Java5开始引入的新特征,类似与.NET 中的attribute.中文名称一般 ...

  7. JSP页面自动刷新

    1.页面自动刷新:把如下代码加入<head>区域中<meta http-equiv="refresh" content="20">,其中 ...

  8. jsp中提示修改成功

    修改成功提示 servert包 request.setAttribute("success", "修改失败"); 效果而 function f(){ var n ...

  9. R Programming week 3-Loop functions

    Looping on the Command Line Writing for, while loops is useful when programming but not particularly ...

  10. DLL线程中坑爹的Synchronize?

    1, 缘起 某次开发语音对讲windows程序,采用delphi语言,及delphix的TDXSound控件. DXSound提供了TSoundCaptureStream类,可以实现指定频率.位数.声 ...