设为首页 收藏本站
查看: 731|回复: 0

[经验分享] Python中字符串常用的方法

[复制链接]

尚未签到

发表于 2017-5-5 06:11:19 | 显示全部楼层 |阅读模式
  capitalize() 方法返回一个字符串的copy,并且这个字符串的首字母大写。例如:

str = "this is string example....wow!!!";
print "str.capitalize() : ", str.capitalize()
#output result
str.capitalize() :  This is string example....wow!!!
  
count() 方法返回子串在指定范围内出现的次数。例如:

str.count(sub, start=0,end=len(string))
   

str = "this is string example....wow!!!";
sub = "i";
print "str.count(sub, 4, 40) : ", str.count(sub, 4, 40)
sub = "wow";
print "str.count(sub) : ", str.count(sub)

#output result
str.count(sub, 4, 40) :  2
str.count(sub, 4, 40) :  1
   endswith() 判断在指定范围内,是否以子串结束。例如:
  startswith()

str = "this is string example....wow!!!";
suffix = "wow!!!";
print str.endswith(suffix);
print str.endswith(suffix,20);
suffix = "is";
print str.endswith(suffix, 2, 4);
print str.endswith(suffix, 2, 6);
#output result
True
True
True
False
    find() 返回子串在指定范围内首次出现的位置,未查到返回-1。例如:

str.find(str, beg=0,end=len(string))
str1 = "this is string example....wow!!!";
str2 = "exam";
print str1.find(str2);
print str1.find(str2, 10);
print str1.find(str2, 40);
#result
15
15
-1
   index()返回子串在指定范围内首次出现的位置,未查到抛出异常。例如:

str.index(str, beg=0end=len(string))

str = "this is string example....wow!!!";
str = "exam";
print str.index(str);
print str.index(str, 10);
print str.index(str, 40);
#result
15
15
Traceback (most recent call last):
File "test.py", line 8, in
print str.index(str, 40);
ValueError: substring not found
isalnum()判断字符串是否全是字母和数字(要么全是字母,要么全是数字,要么全是数字和字母)例如:
str.isa1num()

str = "this2009";  # No space in this string
print str.isalnum();
str = "this is string example....wow!!!";
print str.isalnum();
#result
True
False
isalpha()方法判断字符串内容全是字母。例如:
str.isalpha()

str = "this";  # No space & digit in this string
print str.isalpha();
str = "this is string example....wow!!!";
print str.isalpha();
#result
True
False
isdecimal()和isnumeric()判断字符串是否全是数字,该字符串必须是unicode object。例如:
str.isdecimal()

str = u"this2009";  
print str.isdecimal();
str = u"23443434";
print str.isdecimal();
#result
False
True
 isdigit()判断字符串全部为数字。例如:
str.isdigit()

str = "123456";  # Only digit in this string
print str.isdigit();
str = "this is string example....wow!!!";
print str.isdigit();
#result
True
False
islower()判断字符串中所有的字母是否都是小写。 isupper() 判断字符串中所有的字母是否都是大写。例如:
str.islower()

str = "THIS is string example....wow!!!";
print str.islower();
str = "this is string example....wow!!!";
print str.islower();

#result
False
True
   isspace()判断字符串是否全是空白符,例如:

str.isspace()

str = "     \t\n"; #include tab,space
print str.isspace();
str = "This is string example....wow!!!";
print str.isspace();
#result
True
False
  istitle()判断字符串中,每个单词的首字母是否都是大写。例如:

str.istitle()

str = "This Is String Example...Wow!!!";
print str.istitle();
str = "This is string example....wow!!!";
print str.istitle();
#result
True
False
   join()通过特殊字符把字符串连接起来,例如:

str.join(sequence)

str = "-";
seq = ("a", "b", "c"); # This is sequence of strings.
print str.join( seq );

#result
a-b-c
  len(str) 计算字符串的长度。
  str.lower()把所有的大写字母转成小写。
  str.upper()把所有的小写字母转成大写。
  swapcase() 方法是把字符串中的小写转成大写,大写转成小写。例如

str.swapcase();
str = "this is string example....wow!!!";
print str.swapcase();
str = "THIS IS STRING EXAMPLE....WOW!!!";
print str.swapcase();

#result
THIS IS STRING EXAMPLE....WOW!!!
this is string example....wow!!!
   
  lstrip()去除掉字符串左边规定的字符,默认是空格。例如:
   rstrip()去除掉字符串右边规定的字符,默认是空格。
  strip()去除掉两边规定的字符,默认是空格

str.rstrip([chars])
str.lstrip([chars])
str.strip([chars]);

str = "     this is string example....wow!!!     ";
print str.lstrip();
str = "88888888this is string example....wow!!!8888888";
print str.lstrip('8');

#result
this is string example....wow!!!
this is string example....wow!!!8888888

str = "     this is string example....wow!!!     ";
print str.rstrip();
str = "88888888this is string example....wow!!!8888888";
print str.rstrip('8');

#result
this is string example....wow!!!
88888888this is string example....wow!!!
   
   maketrans()看例子吧:例子中实际上是把对应的字母替换成数字。

str.maketrans(intab, outtab]);

from string import maketrans   # Required to call maketrans function.
intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)
str = "this is string example....wow!!!";
print str.translate(trantab);

#result
th3s 3s str3ng 2x1mpl2....w4w!!!
   
   max()返回字符串中最大的字母。例如:

max(str)

str = "this is really a string example....wow!!!";
print "Max character: " + max(str);
str = "this is a string example....wow!!!";
print "Max character: " + max(str);

#result
Max character: y
Max character: x
  replace()用新字符替换旧字符

str.replace(old,new[, max]) max表示替换的个数

str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");         
print str.replace("is", "was", 3);

#result
thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string
  rfind()返回指定指定范围内,子串最后出现的索引,找不到返回-1。例如:

str.rfind(str, beg=0end=len(string))

str = "this is really a string example....wow!!!";
str1 = "is";
print str.rfind(str1);
print str.rfind(str1, 0, 10);
print str.rfind(str1, 10, 0);
print str.find(str1);
print str.find(str1, 0, 10);
print str.find(str1, 10, 0);
#result
5
5
-1
2
2
-1
   rjust()看例子吧:

str.rjust(width[, fillchar])

str = "this is string example....wow!!!";
print str.rjust(50, '0');

#result
000000000000000000this is string example....wow!!!
  zfill()用“0”进行填充。看例子吧:

str.zfill(width)

str = "this is string example....wow!!!";
print str.zfill(40);
print str.zfill(50);
#result
00000000this is string example....wow!!!
000000000000000000this is string example....wow!!!
  
   
   split()按指定的分隔符分隔字符串,最终返回一个列表。例如:

str.split(str="", num=string.count(str)).num代表分隔的次数

str = "Line1-abcdef \nLine2-abc \nLine4-abcd";
print str.split( );
print str.split(' ', 1 );

#result
['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
['Line1-abcdef', '\nLine2-abc \nLine4-abcd']
   title() 把字符串中每个单词的首字母大写。例如:

str.title();

str = "this is string example....wow!!!";
print str.title();
#result
This Is String Example....Wow!
   translate()看例子吧

str.translate(table[, deletechars]);

from string import maketrans   # Required to call maketrans function.
intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)
str = "this is string example....wow!!!";
print str.translate(trantab, 'xm');
#result
th3s 3s str3ng 21pl2....w4w!!!
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373099-1-1.html 上篇帖子: python解析xml 3 用pyQuery 下篇帖子: Python 入门教程 2 ---- Tip Calculator
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表