python学习笔记——第三章 字符串

第三章 字符串学习

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()一样,不过只能处理单个字符


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值