python教程-使用字符串
Posted share2perfect
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python教程-使用字符串相关的知识,希望对你有一定的参考价值。
一、设置字符串的格式:精简版
方法1
>>> format = "Hello %s, welcome to %s" >>> values = ("tom", "shanghai") >>> format % values
‘Hello tom, Welcome to shanghai‘
方法2
>>> from string import Template >>> tmpl = Template("Hello $who, Welcome to $where") >>> tmpl.substitute(who="tom", where="shanghai")
‘Hello tom, Welcome to shanghai‘
方法3
#无名称无索引 >>> ", and ".format("first", "second","third")
‘first, second and third‘ #有索引 >>> "1,0 and 2".format("second","first","third")
‘first, second and third‘ #有名字 >>> from math import pi >>> "name is approximately value:.2f".format(name="π",value=pi)
‘π is approximately 3.14‘
二、设置字符串的格式:完整版
1.替换字段名
>>>"foo, , , bar".format(5,4,foo=6,bar=9)
‘6, 5, 4, 9‘
>>>"foo, 1, 0, bar".format(5,4,foo=6,bar=9) ‘6, 4, 5, 9‘
>>> familyname = ["liu", "zhang"] >>> "Mr name[0]".format(name=familyname) ‘Mr liu‘
2.基本转换
s、r和a分别使用str、repr和ascii进行转换
>>> print("pi!s pi!r pi!a".format(pi="π")) π ‘π‘ ‘\u03c0‘
可指定转换为的类型
#转为8进制 >>> "the number is num:o".format(num=60) ‘the number is 74‘ #转为百分数 >>> "the number is num:%".format(num=60) ‘the number is 6000.000000%‘ #转为小数,用f >>> "the number is num:.2f".format(num=60) ‘the number is 60.00‘ #转为十六进制,用x >>> "the number is num:x".format(num=60) ‘the number is 3c‘ #转为二进制 用b >>> "the number is num:b".format(num=60) ‘the number is 111100‘
3.宽度、精度和千位分割符
#设置宽度 >>> "num:10".format(num=3) ‘ 3‘ >>> "str:10".format(str="abc") ‘abc ‘
#设置精度 >>> from math import pi >>> "Pi day is pi:.2f".format(pi=pi) ‘Pi day is 3.14‘
#设置宽度和精度 >>> from math import pi >>> "Pi day is pi:10.2f".format(pi=pi) ‘Pi day is 3.14‘
#设置千分位分割符用逗号 >>> "the price of TV is :,".format(3689) ‘the price of TV is 3,689‘
4.符号、对齐和用0填充
#用0填充,宽度保持10位,2位小数 >>> from math import pi >>> ":010.2f".format(pi) ‘0000003.14‘
#左对齐用< >>> print(‘:<10.2f‘.format(pi)) 3.14 #居中用^ >>> print(‘:^10.2f‘.format(pi)) 3.14 #右对齐用> >>> print(‘:>10.2f‘.format(pi)) 3.14
#制定填充字符π >>> print(‘:π^10.2f‘.format(pi)) πππ3.14πππ
#等号=制定填充字符位于符号和数字中间 >>> print(‘:=10.2f‘.format(-pi)) - 3.14 >>> print(‘:10.2f‘.format(-pi)) -3.14
三、字符串方法
#center,字符串两边添加填充字符 >>> "wolaile".center(10) ‘ wolaile ‘ #center,字符串两边添加填充字符,指定字符# >>> "wolaile".center(10, "#") ‘#wolaile##‘ >>>
#find方法,在给定的字符串中查找子串,找到返回index >>> "wolaile".find("ai") 3 #find方法,找不到返回-1 >>> "wolaile".find("aia") -1
#find方法指定起点和终点,第二个参数起点,第三个参数终点 >>> "wolaile shanghai nihao".find("ha",9) 9 >>> "wolaile shanghai nihao".find("ha",10) 13 >>> "wolaile shanghai nihao".find("ha",10,11) -1
#join用于合并序列的元素,跟split相反 >>> seq=[‘1‘,‘2‘,‘3‘,‘4‘,‘5‘] >>> sep=‘+‘ >>> sep.join(seq) ‘1+2+3+4+5‘
>>> dirs = ‘‘,‘usr‘,‘bin‘,‘env‘ >>> dirs (‘‘, ‘usr‘, ‘bin‘, ‘env‘) >>> ‘/‘.join(dirs) ‘/usr/bin/env‘
#lower用于返回字符串的小写 >>> "WOLAILE".lower() ‘wolaile‘ #title 用于首字符大写(单词分割可能会有问题) >>> "that‘s all folks".title() "That‘S All Folks" #capwords也是用于首字符大写 >>> import string >>> string.capwords("this‘s a cat") "This‘s A Cat"
#replace 替换 >>> "this is a cat".replace("cat", "dog") ‘this is a dog‘ #split 用于分割,与join相反 >>> ‘/usr/bin/env‘.split("/") [‘‘, ‘usr‘, ‘bin‘, ‘env‘] #strip 删除字符串的开头和结尾的空白,中间的忽略 >>> " biu biu ".strip() ‘biu biu‘ #strip还可以删除字符串中首尾指定的字符 >>> "###haha # biu $ biu$$$".strip("#$") ‘haha # biu $ biu‘
#translate 作用类似replace,效率比replace高,使用前需创建转换表 >>> table = str.maketrans("cs","kz") >>> table 99: 107, 115: 122 >>> "this is an incredible test".translate(table) ‘thiz iz an inkredible tezt‘
以上是关于python教程-使用字符串的主要内容,如果未能解决你的问题,请参考以下文章