首页 > 编程语言 > 详细

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

时间:2016-02-28 00:47:26      阅读:309      评论:0      收藏:0      [点我收藏+]

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

 

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

原文:http://www.cnblogs.com/tom-li/p/5223886.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!