码迷,mamicode.com
首页 > 编程语言 > 详细

Python学习-str字符串

时间:2016-09-04 15:57:37      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

1.字符串的格式

1 learn_str = "ZiFuChuan" 
2 learn_str = str("ZiFuChuan")

2.字符串的运行原理

1 learn_str = "ZiFuChuan" => "ZiFuChuan" = [Z,i,F,u...]

3.字符串的内置功能

*索引

1 learn_str = "ZiFuChuan"
2 print(learn_str[0])  =>  "Z"
3 print(learn_str[1])  =>  "i"
4 
5 print(learn_str[-1])  =>  "n"
6 print(learn_str[-2])  =>  "a"

*切片

1 learn_str = "ZiFuChuan"
2 print(learn_str[0:3])  =>  "Zi"
3 print(learn_str[:3])  =>  "Zi"
4 
5 print(learn_str[-3:0])  =>  无返回
6 print(learn_str[-3:])  =>  uan
7 print(learn_str[-3:-1])  =>  "ua"
8 print(learn_str[:-1])  => "ZiFuChua"

* "+" 字符串连接

1 learn_str_1 = wo
2 learn_str_2 = ni
3 print(learn_str_1 + learn_str_2)  => woni
4 print(wo + ni) => woni

* "*" 字符串的重复输出

1 learn_str_1 = wo
3 print(learn_str_1 * 3)  => wowowo
4 print(wo * 3) => wowowo

* capitalize() 第一个字母大写

1 learn_str = "ZiFuChuan"
2 print(learn_str.capitalize())  => Zifuchuan

* lower() 所有大写字母变为小写

1 learn_str = "ZiFuChuan"
2 print(learn_str.lower())  =>  zifuchuan

* upper() 所有小写字母变为大写

1 learn_str = "ZiFuChuan"
2 print(learn_str.upper())  =>  ZIFUCHUAN

* center(width,fillchar=None)  内容居中,空白处可填充

1 learn_str = "ZiFuChuan"
2 print(learn_str.center(20))  => "     ZiFuChuan     " 一共20位
3 print(learn_str.center(20,"*"))  =>  "*****ZiFuChuan*****"

*ljust(width,fillchar=None) 内容左对齐,右侧填充 

1 learn_str = "ZiFuChuan"
2 print(learn_str.ljust(20,*))  =>  ZiFuChuan**********

*rjust(width,fillchar=None) 内容右对齐,左侧填充 

1 learn_str = "ZiFuChuan"
2 print(learn_str.rjust(20,‘*‘))  =>  **********ZiFuChuan

*count(sub,strat=None,end=None) 子序列个数 

1 learn_str = "ZiFuChuan"
2 print(learn_str.count("u"))  =>  2
3 print(learn_str.count("u",4))  =>  1
4 print(learn_str.count("u",4,6))  => 0
5 print(learn_str,count("hu")) => 1

*endswith(suffix,start=None,end=None) 是否以子序列结尾 

learn_str = "ZiFuChuan"
print(learn_str.endswith(n))   => True
print(learn_str.endswith(an))   => True
print(learn_str.endswith(i,0,2))   => True

* startswth(suffix,start=None,end=None)  是否以子序列开头

1 learn_str = "http://www.w3cschool.cc/"
2 print(learn_str.startswith(h))  ==》 Thue

*expandtabs(tablesize=None) 将tab转换为空格,默装换为8个空格 

learn_str = "ZiFu\tChuan"
print(learn_str.expandtabs()) => "ZiFu        Chuan"
print(learn_str.expandtabs(2)) => "ZiFu  Chuan"

*find(sub,start=None,end=None) 寻找子序列位置,若未找到返回-1 

learn_str = "ZiFuChuan"
print(learn_str.find("u"))  => 3
print(learn_str.find(iF)) => 1
print(learn_str.find(g)) => -1
print(learn_str.find(F, 1, 3)) => 2 此处区间位置不会按照所在位置取
print(learn_str.find(F, 3, 4)) => -1

*rfind(sub,start=None,end=None) 寻找子序列位置,从右边开始,若未找到返回-1 

1 learn_str = "ZiFuChuan"
2 print(learn_str.find("u"))  => 6

*index(sub,start=None,end=None) 寻找子序列位置,若未找到报错 

1 learn_str = "ZiFuChuan"
2 print(learn_str.index(u))  => 3
3 print(learn_str.index(W)) => 报错

*rindex(sub,start=None,end=None) 寻找子序列位置,从右边开始,若未找到报错 

1 learn_str = "ZiFuChuan"
2 print(learn_str.rindex(u))  => 6
3 print(learn_str.rindex(W)) => 报错

*format() 字符串的格式化 

print(My {} is {}.format(haha,hehe)) => My haha is hehe
print(My {name} is {names}.format(name=haha,names=hehe)) => My haha is hehe

*join(iterable) 连接 

1 learn_str = (a,b,c,d,e)
2 print(‘‘.join(learn_str))  =>  abcde
3 print(-.join(learn_str))  => a-b-c-d-e

*strip(chars=None) 移除字符串两侧空白或指定序列 

1 learn_str = "  ZiFuChuan  "
2 print(learn_str.strip()) => ZiFuChuan  
3 learn_str = "**ZiFuChuan**"
4 print(learn_str.srtip(*)) => ZiFuChuan  

*lstrip(chars=None) 移除字符串左侧空白或指定序列 

learn_str = "  ZiFuChuan  "
print(learn_str.lstrip()) => ZiFuChuan  
learn_str = "**ZiFuChuan**"
print(learn_str.lstrip(*)) => "ZiFuChuan**"

*rstrip(chars=None) 移除字符串右侧空白或指定序列 

1 learn_str = "  ZiFuChuan  "
2 print(learn_str.rstrip()) =>   ZiFuChuan
3 learn_str = "**ZiFuChuan**"
4 print(learn_str.rstrip(*)) => "**ZiFuChuan"

*partition(sep) 分割前中后三个部分 

1 str = "http://www.w3cschool.cc/"
2 print(str.partition(://))
3 ==> [http :// www.w3cschool.cc/‘]

*rpartition(sep) 分割前中后三个部分,从右侧检测

1 learn_str = "http://www.w3cschool.cc/"
2 print(learn_str.rpartition(w))
3 ==> [http://www. w 3cschool.cc/‘]

*split(str="",num=srting.count(str)) 通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串

1 learn_str = "http://www.w3cschool.cc/"
2 print(learn_str.split("w"))
3 ==> [http://, ‘‘, ‘‘, ., ‘‘, 3cschool.cc/‘]
4 print(learn_str.split("w",2))
5 ==> [http://, ‘‘, w.w3cschool.cc/‘]

*rsplit(str="",num=srting.count(str)) 从右侧开始检索,通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串

learn_str = "http://www.w3cschool.cc/"
print(learn_str.rsplit("w"))
 ==》 [http://, ‘‘, ‘‘, ., 3cschool.cc/]
print(learn_str.rsplit("w",2))
 ==》 [http://ww, ., 3cschool.cc/]

* replace(old,new,count=None) 替换字符列 

1 learn_str = "ZiFuChuan"
2 print(learn_str.replace(u,s))    => ZiFsChsan
3 print(learn_str.replace(Fu,s))  => ZisChuan
4 print(learn_str.replace(u,s,1))  => ZiFsChuan 
5 ==>count参数表示替换的个数

* translate(table,deletechars=None) 转换,需要先做一个对应表。

1 intab = aeiou
2 outtab = 12345
3 trantab = str.maketrans(intab, outtab)
4 
5 learn_str = this is string example...wow!!!
6 print(learn_str.translate(trantab))
7 ==》th3s 3s str3ng 2x1mpl2...w4w!!!

* zfill 返回指定长度的字符串,原字符串右对齐,前面填充0。

1 learn_str = "ZiFuChuan"
2 print(learn_str.zfill(15)) => "000000ZiFuChuan"

* decode() 解码 

* encode() 编码针对unicode 

* isalnum() 是否是字母和数字,返回布尔值

* isalpha() 是否是字母,返回布尔值

* isalgif() 是否是字母,返回布尔值

* islower() 是否小写,返回布尔值

 

Python学习-str字符串

标签:

原文地址:http://www.cnblogs.com/guantouping/p/5839433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!