python学习笔记——第三章 字符串
2014-04-19 00:10
393 查看
第三章 字符串学习
1、字符串不可修改, 无法进行分片赋值
1、字符串不可修改, 无法进行分片赋值
>>> format = "hello, %s. %s enough for ya?" >>> values = ('world','hot') >>> print (format % values) #在%的左侧放置一个字符串(格式化字符串),右侧放置希望格式化的值 hello, world. hot enough for ya? >>> format = "Pi with three decimals: %.3f" #.3表示希望保留的小数位数,f表类型(浮点数) >>> from math import pi >>> print(format %pi) Pi with three decimals: 3.142 模板字符串 string--Template---substitute >>> # 模板字符串 >>> from string import Template >>> s=Template('$x,glorious $x!') >>> s.substitute(x='slurm') 'slurm,glorious slurm!' >>># 如果替换字段是单词的一部分,需要加{} >>> s=Template("It's ${x}tastic!") >>> s.substitute(x='slurm') "It's slurmtastic!" >>> #可以使用$$插入美元符号 >>> # 使用字典变量提供值/名称对 >>> s=Template('A $thing must never $action.') >>> d={} >>> d['thing']='gentleman' >>> d['action']='show his socks' >>> s.substitute(d) 'A gentleman must never show his socks.' 2、字符串格式化 >>> '%s plus %s equals %s' %(1,1,2) # #使用元组替代,注意不能丢掉括号 '1 plus 1 equals 2' >>> 'Price of eggs: $%d' % 42 #d,i表示带符号的十进制整数 'Price of eggs: $42' >>> 'Hexadecimal price of eggs: %x' % 42 #不带符号的十六进制(小写),X(大写) 'Hexadecimal price of eggs: 2a' >>> from math import pi >>> 'Pi: %f...' % pi #f/F十进制浮点数 'Pi: 3.141593...' >>> 'Very inexact estimate of pi: %i' %pi #i带符号的十进制整数 'Very inexact estimate of pi: 3' 3、段宽度和精度 >>>#宽度:转换后的值所保留的最小字符个数。 >>>#精度:应包含的小数位数,或者是转换后最大字符个数。(字符串) >>> '%10f' % pi #字段宽度10 ' 3.141593' >>> '%10.2f' %pi #字段宽度10 精度2 ' 3.14' >>> '%.2f' %pi #精度2 '3.14' >>> '%.5s' % 'hello python' #精度5,对于字符串来说,就是最多五个字符 'hello' >>> '%.*s' %(5,'hello python') #用*表示精度,值在元组中 'hello' 4、符号、对齐和0填充--在字段宽度和精度之前可以放置一个标表,该标表可以是零、加号、减号或空格 >>> from math import pi >>> '%10f'% pi ' 3.141593' >>> '%010.2f' %pi '0000003.14' >>> '%-10.2f' % pi # -表示左对齐 '3.14 >>> print(('% 5d' %10) + '\n' + ('%5d' %-10)) #空格,在正数前加空白,方便与负数对齐 10 -10 >>> print(('%+5d' % 10)+'\n' + ('%+5d' % -10)) #(+)加号,正数和负数都标出符号 +10 -10 5、字符串方法 # find方法可以在一个较长的字符串中查找子字符串,返回子串所在位置的最左端索引。没找到返回-1 >>> title="Monty python's Flying Circus" >>> title.find('Monty') 0 >>> title.find('python') 6 >>> subject = '$$$ Get rich now!!! $$$' >>> subject.find('$$$') 0 >>> subject.find('$$$', 1) #提供起点,从1开始 20 >>> subject.find('!!!', 0, 16) #提供起始点和结束点,其中包括起始索引,不包括结束索引 -1 # -1代表未找到 # join方法是split方法的逆方法,用来连接列表,只能是字符串 >>> seq=['1','2','3','4','5'] >>> sep = '+' >>> sep.join(seq) '1+2+3+4+5' >>> >>> dirs = '', 'usr', 'bin', 'env' >>> '/' .join(dirs) '/usr/bin/env' >>> print('C:' + '\\'.join(dirs)) C:\usr\bin\env #lower方法返回字符串的小写字母版 >>> 'SHE IS TALL'.lower() 'she is tall' #title方法将字符串的首字母转换为大写,其他字母小写 >>> "that's all folks".title() "That'S All Folks" #String模块的capwords函数:将所有字母都转换为大写 >>> import string >>> string.capwords("that's all folks") "That's All Folks" >>> #replace方法返回某字符串的所有匹配项均被替换之后得到的字符串 >>> 'This is a test'.replace('is', 'eez') 'Theez eez a test' #split方法是join的逆方法,用来将字符串分割成序列 >>> '1+2+3+4+5+6+7+8+9'.split('+') ['1', '2', '3', '4', '5', '6', '7', '8', '9'] >>> '/usr/bin/env'.split() ['/usr/bin/env'] >>> '/usr/bin/env'.split('/') ['', 'usr', 'bin', 'env'] >>> 'Using the default'.split() ['Using', 'the', 'default'] #strip方法返回去除两侧(不包括内部)空格的字符串 >>> ' internal whitespace is kept '.strip() 'internal whitespace is kept' #translate()同replace()一样,不过只能处理单个字符
相关文章推荐
- Python学习笔记-第三章 使用字符串
- Python基础教程学习笔记----第三章 字符串
- python2学习笔记 第三章 使用字符串
- python基础教程学习笔记 第三章 字符串
- 编程小白的第一本 python 入门书 学习笔记02 第三章 变量与字符串
- 【Python学习笔记】-字符串和字典
- Python学习笔记,5,字符串和编码问题
- Python学习笔记三 字符串
- python 学习笔记 -- 字符串、数值及运算符
- Data Analysis学习笔记 --- Python数据清洗对字符串处理
- Python学习笔记——字符串
- python学习笔记1-字符串的使用
- Python学习笔记(三)字符串
- Python学习第三章《字符串》(成为高手前必须知道的一些基础知识)
- python3.4学习笔记(十五) 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- python学习笔记:字符串操作大全
- Python学习笔记-Python的字符串,格式化,条件判断,循环、raw_input的注意事项
- [python]学习笔记3-字符串内建函数2
- Python学习笔记---字符串
- 学习笔记之Python 切片:利用切片操作,实现一个trim()函数,去除字符串首尾的空格