第二天 Python3.4.2 字符串的格式化 和 常用操作

1. 字符串是一个线性结构

重要:字符串是不可变的。

'I love %s' %('Python')
'I love %s' %('Python')
​
Out[5]:
'I love Python'
In [6]:

'I love %s' %('Python',)
Out[6]:
'I love Python'
In [7]:

'I love %(name)s' %{'name':'Python'}
Out[7]:
'I love Python'
In [14]:

'I love %(name)s,%(name)s is my lang' %{'name':'Python'}  #这个地方是{} 大括号
Out[14]:
'I love Python,Python is my lang'
In [15]:

'I love %s,%s is my lang' %('Python','Python')
Out[15]:
'I love Python,Python is my lang'

 字符串格式化输出字符图片的的说明:

In [46]: '%d' %3.4
Out[46]: '3'

In [47]: '%E'%0.000000001
Out[47]: '1.000000E-09'

In [48]: '%E'%1110000000000000000000000
Out[48]: '1.110000E+24'

In [49]: '%g' %0.0001
Out[49]: '0.0001'

In [50]: '%g' %0.000000000000000000001
Out[50]: '1e-21'

 

字符串的flags:填充

In [51]: '%10d' %1
Out[51]: '         1'

In [52]: '%010d' %1
Out[52]: '0000000001'

 

二.字符串的常用操作:format方法

这个表很直观:

In [53]: s = "URL:http://www.google.com"
In [54]: key,value = s.split(":",1)  #以:为分隔符,去第一个

In [55]: print(key,value)
URL http://www.google.com

In [56]: s.split(':')
Out[56]: ['URL', 'http', '//www.google.com']

 

splitlines 的使用:分割字符串使用True

In [61]: s="\nI love python\nI also love linux\n"

In [62]: s
Out[62]: '\nI love python\nI also love linux\n'

In [63]: s.spli
s.split       s.splitlines  

In [63]: s.splitlines()  #默认是False
Out[63]: ['', 'I love python', 'I also love linux']

In [64]: s.splitlines(True)
Out[64]: ['\n', 'I love python\n', 'I also love linux\n']

In [65]: s.splitlines(False)
Out[65]: ['', 'I love python', 'I also love linux']

In [66]: help(s.splitlines)
Help on built-in function splitlines:

splitlines(...) method of builtins.str instance
S.splitlines([keepends]) -> list of strings

Return a list of the lines in S, breaking at line boundaries.
Line breaks are not included in the resulting list unless keepends
is given and true.

 

分割:partition

In [1]: s='root:x:0:0:root:/root:/bin/bash'

In [2]: s.rpartition(':')  #右边第一个:分割
Out[2]: ('root:x:0:0:root:/root', ':', '/bin/bash')

In [4]:  h,_,t=s.partition(':') #以左边第一个:分割  ,_为占位符不骑

In [5]: t
Out[5]: 'x:0:0:root:/root:/bin/bash'

In [6]: h
Out[6]: 'root'

 

 

字符串的修改:

In [8]: s="Python"

In [9]: s.center(20)
Out[9]: '       Python       '

In [10]: s.center(20,'*')
Out[10]: '*******Python*******'

 

字符串去除空格与换行:strip

In [16]: s=s.center(20)

In [17]: s
Out[17]: '       Python       '

In [18]: s.strip()
Out[18]: 'Python'

In [19]: s='abc\n'

In [20]: s.strip()
Out[20]: 'abc'

 

过滤root的bash:

In [23]: for line in f.readlines():
        line = line.strip()
        if line.startswith('root:'): 
                _,shell = line.rsplit(':',1)
                print (shell)
   ....:         
/bin/bash

 

替换:replace

In [25]: s="root:x:0:0:root:/root:/bin/bash"

In [26]: s
Out[26]: 'root:x:0:0:root:/root:/bin/bash'

In [27]: s.replace('root','lzt')
Out[27]: 'lzt:x:0:0:lzt:/lzt:/bin/bash'

In [28]: s.replace('root','lzt',1)
Out[28]: 'lzt:x:0:0:root:/root:/bin/bash'

In [29]: s.replace('root','lzt',-1)
Out[29]: 'lzt:x:0:0:lzt:/lzt:/bin/bash'

In [30]: s
Out[30]: 'root:x:0:0:root:/root:/bin/bash'

 

 课堂总结截图:

Python的规范是:pep8

 

posted @ 2016-02-27 23:51  Tom-Li  阅读(373)  评论(0编辑  收藏  举报