持续更新:Python-string函数例子

#P48
import string;
str_old="this Is test";
str_space=" this Is test ";
str_tab="this\tIs test";
str_split="this,Is,test,programe";
str_num="123456";
str_numandstr="a1234";
str_notnumandstr="a123!4";
str_string="allstring";
str_space=" ";
str_tab="\t";
print("原始字符串:",str_old);
print("================================================================");
#全部转换成小写
print("lower:",str_old.lower());


#全部大写
print("upper:",str_old.upper());
#大小写互换
print("swapcase:",str_old.swapcase());


#首字母大写
print("capitalize:",str_old.capitalize());


#只有首字母大写,其余为小写
print("title:",str_old.title());


#在它把 字符串 用split()函数分开,然后用capitalize()把首字母变成大写,最后用join()合并到一起
print(string.capwords("this is String.capwordsd","==="));


#输出width个字符,S字符障蔽30位,左边不足部分用fillchar填充,默认的为空格。
#S.ljust(width,[fillchar])
print("ljust:",str_old.ljust(30,"-"));


#输出width个字符,S字符障蔽30位,不足部分用fillchar填充,中间对齐 填充默认的为空格
#S.center(width, [fillchar])

print("center:",str_old.center(30,"-"));

#输出width个字符,S字符障蔽30位,右边不足部分用fillchar填充,默认的为空格。
#S.rjust(width,[fillchar])
print("rjust:",str_old.rjust(30,"-"));


#输出width个字符,并在右对齐,不足部分用0补足
#S.center(width, [fillchar])
print("zfill:",str_old.zfill(30));


#返回S中出现substr的第一个字母的标号,如果S中没有substr则返回-1。start和end作用就相当于在S[start:end]中搜索
#S.find(substr, [start, [end]])
print("================================================================");
print("find:",str_old.find("is"));
print("find,指定STAR:",str_old.find("is",3));
print("find,指定STAR和END:",str_old.find("is",0,5));
print("================================================================");
#S.index(substr, [start, [end]])
#与find()相同,只是在S中没有substr时,会返回一个运行时错误
print("================================================================");
print("index:",str_old.index("s"));
print("index,指定STAR:",str_old.index("s",5));
print("index,指定STAR和END:",str_old.index("s",0,5));
print("================================================================");
#S.rfind(substr, [start, [end]])
#返回S中最后出现的substr的第一个字母的标号,如果S中没有substr则返回-1,也就是说从右边算起的第一次出现的substr的首字母标号
print("================================================================");
print("rfind:",str_old.rfind("s"));
print("rfind,指定STAR:",str_old.rfind("s",5));
print("rfind,指定STAR和END:",str_old.rfind("s",0,5));
print("================================================================");
#S.count(substr, [start, [end]])
#计算substr在S中出现的次数
print("================================================================");
print("count:",str_old.count("s"));
print("count,指定STAR:",str_old.count("s",5));
print("count,指定STAR和END:",str_old.count("s",0,5));
print("================================================================");
#S.replace(oldstr, newstr, [count])
#把S中的oldstar替换为newstr,count为替换次数。这是替换的通用形式,还有一些函数进行特殊字符的替换
print("================================================================");
print("replace:",str_old.replace("s","替换s"));
print("replace,指定次数-2:",str_old.replace("s","替换s2次",2));
print("================================================================");
#S.strip([chars])
#把S中前后chars中有的字符全部去掉,可以理解为把S前后chars替换为None
print("================================================================");
print("strip:",str_space.strip());
print("strip,指定替换字符:",str_space.strip("t"));
print("================================================================");
#把S中前chars中有的字符全部去掉,可以理解为把S前后chars替换为None
print("================================================================");
print("lstrip:",str_space.lstrip());
print("lstrip,指定替换字符:",str_space.lstrip("t"));
print("================================================================");
#把S中后chars中有的字符全部去掉,可以理解为把S前后chars替换为None
print("================================================================");
print("rstrip:",str_space.rstrip());
print("rstrip,指定替换字符:",str_space.rstrip("t"));
print("================================================================");
#S.expandtabs([tabsize])
#把S中的tab字符替换没空格,每个tab替换为tabsize个空格,默认是8个
print("expandtabs:",str_tab.expandtabs(30));
#S.split([sep, [maxsplit]])
#以sep为分隔符,把S分成一个list。maxsplit表示分割的次数。默认的分割符为空白字符即空格
print("================================================================");
print("split:",str_split.split());
print("split',':",str_split.split(","));
print("split','分隔2次:",str_split.split(",",2));
print("================================================================");
#S.splitr([sep, [maxsplit]])
#从右边开始分隔
print("================================================================");
print("rsplit:",str_split.rsplit());
print("rsplit',':",str_split.rsplit(","));
print("rsplit','分隔2次:",str_split.rsplit(",",2));
print("================================================================");
#S.join(seq)
#把seq代表的序列──字符串序列,用S连接起来
print("join:",str_split.join({"-","join","-","string"}));
#S.encode([encoding,[errors]])
# 其中encoding可以有多种值,比如gb2312 gbk gb18030 bz2 zlib big5 bzse64等都支持。
#errors默认值为"strict",意思是UnicodeError。
#可能的值还有'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace'
#和所有的通过codecs.register_error注册的值。
str_code="test-code";
print("encode:",str_code.encode("gb18030","ignore"));
#S.startswith(prefix[,start[,end]]) ,endswith与startswith类似
#是否以prefix开头
print("================================================================");
print("startwith:",str_old.startswith("th"));
print("startwith,指定STAR:",str_old.startswith("th",5));
print("startwith,指定STAR和END:",str_old.startswith("th",5,8));
print("================================================================");
#S.isalnum()
#是否全是字母和数字,并至少有一个字符
print("================================================================");
print("isalnum(纯数字):",str_num.isalnum());
print("isalnum(字母及数字):",str_numandstr.isalnum());
print("isalnum(字母及数字及特殊字符):",str_notnumandstr.isalnum());
print("================================================================");
#S.isalpha()
#是否全是字母,并至少有一个字符
print("================================================================");
print("isalpha(纯字母):",str_string.isalpha());
print("isalpha(字母及数字):",str_numandstr.isalpha());
print("================================================================");
#S.isdigit()
#是否全是数字,并至少有一个字符
print("================================================================");
print("isdigit(纯数字):",str_num.isdigit());
print("isdigit(字母及数字):",str_numandstr.isdigit());
print("isdigit(字母及数字及特殊字符):",str_notnumandstr.isdigit());
print("================================================================");
#S.isspace()
#是否全是空白字符,并至少有一个字符
print("isspace(空格字符串):",str_space.isspace());
print("isspace(TAB字符串):",str_tab.isspace());
print("isspace(字母及数字及特殊字符):",str_notnumandstr.isspace());
#S.islower()
#S中的字母是否全是小写,S.isupper(),S.istitle()与此类似
print("islower(全为小写)test:","test".isspace());
print("islower(全为大写)TEST:","TEST".isspace());
print("islower(大小写混合):Test","Test".isspace());
print("islower(小写+特殊字符)test!:","test!".isspace());
print("islower(大写+特殊字符)TEST!:","TEST!".isspace());
print("islower(大写+小写+特殊字符)Test!:","Test!".isspace());

#string.atoi(s[,base])
#base默认为10,如果为0,那么s就可以是012或0x23这种形式的字符串,如果是16那么s就只能是0x23或0X12这种形式的字符串
#string.atol(s[,base]) #转成long
#string.atof(s[,base]) #转成float

 

你可能感兴趣的:(python)