您的位置:首页 > 编程语言 > Python开发

python学习笔记——第三章 串

2015-08-24 08:49 573 查看
第三章 字符串学习

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()如,但只能处理一个字符
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: