Python中字符串常用的方法

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!!!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值