python字符串常用方法
1、isalnum():判断字符串所有的字符都是字母或者数字。返回true和false
In[1]:str1='jiangwei520' In[2]:str2='jiangwei' In[3]:str3='520' In[4]:str4='5201314' In[5]:str1.isalnum() Out[5]:True In[6]:str2.isalnum() Out[6]:False In[7]:str3.isalnum() Out[7]:True In[8]:str4.isalnum() Out[8]:False
2、isalpha():判断字符串所有的字符都是字母。返回true和false
In[11]:s1='jw' In[12]:s2='jw' In[13]:s1.isalpha() Out[13]:False In[14]:s2.isalpha() Out[14]:True
3、isdigit():判断字符串所有的字符都是数字。返回true和false
In[15]:n1='1234' In[16]:n2='1234' In[17]:n3='1.1' In[18]:n1.isdigit() Out[18]:False In[19]:n2.isdigit() Out[19]:True In[20]:n3.isdigit() Out[20]:False
4、islower():判断所有的字符都是小写。
In[23]:s1='jw' In[24]:s2='jw' In[25]:s3='JW' In[26]:s1.islower() Out[26]:True In[27]:s2.islower() Out[27]:True In[28]:s3.islower() Out[28]:False
5、isupper():判断所有的字符都是大写。
In[29]:s1='Jw' In[30]:s2="JW" In[31]:s3="JW" In[32]:s4='Jw' In[33]:s1.isupper() Out[33]:False In[34]:s2.isupper() Out[34]:True In[35]:s3.isupper() Out[35]:True In[36]:s4.isupper() Out[36]:False
6、istitle():判断每个单词的首字母都是大写。
In[37]:s1='helloworld' In[38]:s2='HelloWorld' In[39]:s3='Hello,world' In[40]:s4='HELLOWORLD' In[41]:s1.istitle() Out[41]:False In[42]:s2.istitle() Out[42]:True In[43]:s3.istitle() Out[43]:False In[44]:s4.istitle() Out[44]:False
7、lower():转小写
In[47]:s4 Out[47]:'HELLOWORLD' In[48]:s4.lower() Out[48]:'helloworld' In[49]:s2 Out[49]:'HelloWorld' In[50]:s2.lower() Out[50]:'helloworld'
7、upper():转大写
In[54]:s1 Out[54]:'HElloWOrld' In[55]:s3 Out[55]:'Hello,world' In[56]:s1.upper() Out[56]:'HELLOWORLD' In[57]:s3.upper() Out[57]:'HELLO,WORLD'
8、strip([chars]):去除
lstrip()和rstrip()类似 In[61]:s1='helloworld!!!' In[62]:s1.strip() Out[62]:'helloworld!!!' In[63]:s2='****jw---love---you****' In[64]:s2.strip('*') Out[64]:'jw---love---you' #应该是去除两边的 In[107]:a='***111***' In[108]:a.lstrip('*') Out[108]:'111***' In[109]:a.rstrip('*') Out[109]:'***111' In[110]:a.strip('*') Out[110]:'111'
9、replace(old,new,[count]):替换
In[72]:a='小喵和小九' In[73]:a.replace('喵','喵喵').replace('九','九九') Out[73]:'小喵喵和小九九' In[74]:b='jiangweiisagoodgoodgoodboy' In[75]:b.replace('good','nice') Out[75]:'jiangweiisaniceniceniceboy' In[76]:b.replace('good','nice',2) Out[76]:'jiangweiisanicenicegoodboy' In[77]:b.replace('good','nice',1) Out[77]:'jiangweiisanicegoodgoodboy'
10、split():切割。返回列表。
In[92]:path1 Out[92]:'a/b/c/d' In[93]:path1.split('/') Out[93]:['a','b','c','d'] In[88]:path='/home/centos/python3.6' In[89]:path Out[89]:'/home/centos/python3.6' In[90]:path.split('/') Out[90]:['','home','centos','python3.6']
11、startswith():以指定的字符串开头。发货true和false。
endswith():类似 In[94]:a='helloworld' In[95]:b='helloworld' In[96]:a.startswith('hello') Out[96]:True In[97]:b.startswith('hello') Out[97]:True In[98]:b.startswith('he') Out[98]:True
12、format():格式化输出
In[111]:print('{name}---->{age}'.format(name='xjm',age=21)) xjm---->21 In[112]:
13、title():把每个字符串的首字母大写
In[112]:s='helloworldpython' In[113]:s.title() Out[113]:'HelloWorldPython' #与capitalize不同。就第一个单词的首字母大写 In[128]:a='helloworldpython' In[129]:a.capitalize() Out[129]:'Helloworldpython'
14、join():插入
In[117]:a='.' In[118]:a.join('jwlove') Out[118]:'j.w.l.o.v.e' In[124]:a='/' In[125]:b=('user','local') In[127]:a.join(b) Out[127]:'user/local'
15、center(width,char):扩充
In[137]:a='Linux' In[138]:a.center(25,'*') Out[138]:'**********Linux**********' In[140]:a.center(25) Out[140]:'Linux' #ljust和rjust类似 In[142]:a.ljust(10,'-') Out[142]:'Linux-----' In[143]:a.rjust(10,'-') Out[143]:'-----Linux'
16、splitlines():根据\r \n \r\n 切割。返回列表
In[151]:a='如果\n没有\r如果' In[154]:print(a) 如果 如果 In[157]:a.splitlines() Out[157]:['如果','没有','如果']
17、format_map():格式化输出
In[158]:a='helloworld{course}' In[160]:course1='python' In[161]:course2='java' In[178]:a.format(course=course1) Out[178]:'helloworldjava' In[179]:a.format(course=course2) Out[179]:'helloworldpython' In[181]:a.format_map(vars()) Out[181]:'helloworldpython'
总结
以上所述是小编给大家介绍的python字符串常用方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!