列表、字符串和字典是Python中最重要的三种数据类型。
Python中字符串是不可改变的,所以像对元组那样对它进行分片赋值是不合理的。
下面我们介绍一些Python基础字符串操作:
1.字符串格式化
使用求模操作符%可以将其他的值转换为包含转换标志的字符串。还可以用来对值进行不同方式的格式化,比如左右对齐,设置宽度精度,增加符号等操作。
2.字符串方法
字符串有很多中方法,在下面的例子中会一一介绍。
好,下面直接捞干的,直接上例子:
# -*- coding: GBK -*- #首先用于确定编码,加上这句 print "~~~~~~~~~~~~~~~~~~~~~~~~~~1~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" #请记住:字符串是不可改变的,所以对字符串进行分片赋值是不合法的 #但是可以使用格式化操作符(%)对字符串进行格式化,左侧为想格式化的字符串,右侧为元组 #%s称为转换说明符,标记需要插入转化值的位置 #若要打印%,则需要连续写两个%% old = "%s ! My %% little %s." new = ("Hello",'brother') print old % new print "~~~~~~~~~~~~~~~~~~~~~~~~~~2~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" #还可以使用格式化操作符进行精度控制(%.数字f),表示保留几位小数 from math import pi word = "PI is equals to: %.10f" print word % pi print "~~~~~~~~~~~~~~~~~~~~~~~~~~3~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" #还可以使用模版字符串对字符串进行格式化,string.substitute(tihuan = 'newString') #使用tihuan字符串的值newString替换string中的$tihuan #若要打印$,则需要连续写两个$$ ##from string import Template ##s = Template('Two $one or not two $one ,you are a $two') ##d = {} ##d['one'] = 'B' ##d['two'] = '2B' ##s.substitute(d) ##print s print "~~~~~~~~~~~~~~~~~~~~~~~~~~4~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" #以下是字符串格式化完整版,%后的参数表示不同含义,其中s表示转换成字符串,f表示十进制浮点数等 print '%s plus %s equals %s' % (1,1,2) print 'Price of eggs: $%d' % 42 #对浮点数进行宽度和精度设置(%宽度.精度f) print '%10f' % pi print '%10.2f' % pi print '%10.19f' % pi print 'Hello , %.6s' %'ZhenyuJiang' #可以从元组中读取所取字符串的长度 print 'Hello , %.*s' %(6,'ZhenyuJiang') print "~~~~~~~~~~~~~~~~~~~~~~~~~~5~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" #可以用空格,0,+,-对字段的长度进行填充,在宽度之前加上这一标表 #0表示将字段所有剩余部分用0填充 print '%020.5f' % pi #-用来左对齐数值,让空格在右面 print '%-20.5f' % pi #+表示无论正数还是负数,都标出符号 print ('%+10d' %10) + '\n' + ('%+10d' %-10) #空格表示在正数之前加上一个空格,用于对齐正负数 print ('% 5d' %10) + '\n' + ('% 5d' %-10) print "~~~~~~~~~~~~~~~~~~~~~~~~~~6~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" #一个利用标表让格式对齐的例子 width = input('Please enter width big than 20:') price_width = 10 item_width = width - price_width header_format = '%-*s%*s' formats = '%-*s%*.2f' print '=' * width print header_format % (item_width,"Item",price_width,'Price') print '-' * width print formats % (item_width,'Apples',price_width,5.2) print formats % (item_width,'Pears',price_width,10.23) print formats % (item_width,'Good Television',price_width,4455.299) print formats % (item_width,"Zhenyu's talk",price_width,5000000.22) print formats % (item_width,'My little dog',price_width,1.2) print formats % (item_width,'hell',price_width,0.2) print '=' * width print "~~~~~~~~~~~~~~~~~~~~~~~~~~7~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" #字符串的一些函数操作之find,在字符串中查找子串,并返回子串位置的最左端索引,没找到返回-1 print 'Dumplings is my favorite food.'.find('ling') print 'Dumplings is my favorite food.'.find('you') #还可以添加起始点和结束点参数 subject = '$$$ Get rich now!!! $$$' print subject.find('$$$') #只有起始点,表示从索引1开始,如果有起始点有结束点则包含起始点位置,不包含结束点索引(前含后不含) print subject.find('$$$',1) print subject.find('!!!',0,18) print subject.find('!!!',0,19) print "~~~~~~~~~~~~~~~~~~~~~~~~~~8~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" #join方法用来在队列中添加元素,将字符串列表连接起来 sep = '+' seq = ['1','2','3','4','5'] print sep.join(seq) dirs = ['','user','lib','bin','view'] print '/'.join(dirs) print 'C:' + '\\'.join(dirs) print "~~~~~~~~~~~~~~~~~~~~~~~~~~9~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" #lower方法返回字符串的小写字母版,title是将字符串转化为标题格式,即单词首字母大写 name = "ZhenYuLi" names = ['zhenyuli','huipu','xiaoxinxin'] if name.lower() in names : print 'found you' print 'this is a small title'.title() #replace方法用于将字符串中的匹配项进行替换并返回替换后的结果 print 'this is a replace test'.replace('test','what?') print "~~~~~~~~~~~~~~~~~~~~~~~~~~10~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~" #split用来将字符串分割成序列,与join操作反过来,不添加参数表示用空格(制表,换行)进行分割 print 'This is a small test by zhenyu'.split() print '1+2+3+4+5'.split('+') #strip方法返回去除两侧空格的字符串(比如输入的用户名或密码两侧有空格),也可指定去除的参数 print ' test if the blank is out '.strip() print '****** test if the * word is out !!!!!******'.strip(' *!') #translate方法只用于处理单个字符的替换,而不是replace的多个字符替换,需要完成一个转换表 #maketrans有两个参数,把c都变成k,s变成z from string import maketrans table = maketrans('cs','kz') print 'this is an incredible test'.translate(table) print "~~~~~~~~~~~~~~~~~~~~~~~~~~END~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" #为了避免双击的时候直接一闪退出,在最后面加了这么一句 raw_input("Press <Enter> To Quit!")
最后列出前两篇Python总结:Python基础学习教程(一)之字符串的秘密
Python基础学习教程(二)之序列的宝藏