Python 字符串操作函数二

匿名 (未验证) 提交于 2019-12-02 22:56:40
#-*- coding:utf-8 -*-  line = "l want watch movie with you ."  print(line.center(50)) print(line.ljust(50)) print(line.rjust(50))  #center 字符串居中 #ljust 字符串居左 #rjust 字符中居右  #lstrip 删除字符串左边的空白字符 #rstrip 删除字符串右边的空白字符 #strip 删除字符串两端的空白字符  word = "\n \t  i am a bird . \n \r "  print("==lstrip==") print(word.lstrip())   print("===rstrip==") print(word.rstrip())   print("==strip==") print(word.strip())  #空白字符包括空格 /n /t等不可见字符   nword = "hello this a new world ." print("==partition==") #partition print(nword.partition("a")) #在源字符串中从左向右查找指定字符串,找到指定字符串做切割,分为3部分,指定字符串前,后,其本身,返回值是元组 #如果指定字符串不存在于源字符串中,那么结果由源字符串本身,两个空字符串组成  #rpartition 从右向左查找指定字符串   wline = "hello \n world" print("==splitlines==") print(wline.splitlines()) #splitlines 按照行进行分割,返回一个包含各行作为元素 的列表   print("==isalpha==") print(wline.isalpha())  #返回false 因为存在空格,转义字符 #isalpha 判断字符串中所有字符是否都是字母,字母返回true,其他返回false   num = "12233" print("==isdigit==") print(num.isdigit()) #isdigit 判断字符串中所有字符是否都是数字,是返回true,其他返回false   str="sadfa213" print("==isalnum==") print(str.isalnum()) #isalnum 字符串是否只由数字和字母组成,是返回true,其他返回false   space="  " print("==isspace==") print(space.isspace()) #isspace 判断字符串中是否只有空格,是返回true,其他返回false   print("==join==") tstr = "_" list1 = ["aa","bb"] print(tstr.join(list1)) #每个字符后面都插入指定的字符,构成一个新的字符串

 

原文:https://www.cnblogs.com/zhanggaofeng/p/9286508.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!