码迷,mamicode.com
首页 > 编程语言 > 详细

python教程-(三)使用字符串

时间:2019-06-02 11:42:16      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:作用   字符串   split   删除   python   忽略   sub   tmp   空白   

一、设置字符串的格式:精简版

方法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教程-(三)使用字符串

标签:作用   字符串   split   删除   python   忽略   sub   tmp   空白   

原文地址:https://www.cnblogs.com/share2perfect/p/10958959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!