Python字符串和其常用函数
Mar丶流年 人气:01.字符串定义
# coding:utf-8 if __name__ == '__main__': ''' 通过单引号定义 通过三个单引号定义 通过三个双引号定义 ''' a = 'this is string' b = '''this is string''' c = """this is string""" print(a, b, c)
2.首字母大写
# coding:utf-8 if __name__ == '__main__': ''' string.capitalize() 将字符串首字母大写 ''' a = 'i am xieruixiang' print(a.capitalize()) # I am xieruixiang
3.所有字母大写
# coding:utf-8 if __name__ == '__main__': ''' string.upper() 将字符串中所有字母大写 ''' a = 'i am xieruixiang' print(a.upper()) # I AM XIERUIXIANG
4.所有字母小写
# coding:utf-8 if __name__ == '__main__': ''' string.lower(),string.casefold() 都能将字符串中所有字母小写 casefold是lower的加强版 ''' a = 'I AM XIERUIXIANG' print(a.lower()) # i am xieruixiang print(a.casefold()) # i am xieruixiang
5.大小写颠倒
# coding:utf-8 if __name__ == '__main__': ''' string.swapcase() 能将字符串中的大写变小写,小写变大写。两极反转 ''' a = 'i AM xIErUIxIANG' print(a.swapcase()) # I am XieRuiXiang
6.填充0至指定长度
# coding:utf-8 if __name__ == '__main__': ''' string.zfill(width) 用0填充字符串到指定width长度,如果字符串长度大于等于width则不填充 ''' a = 'abcdef' print(a.zfill(6)) # abcdef print(a.zfill(10)) # 0000abcdef
7.统计字符串中某个成员的个数
# coding:utf-8 if __name__ == '__main__': ''' string.count(item) 统计字符串中有几个item ''' a = 'abaacdef' print(a.count('a')) # 3 print(a.count('aa')) # 1 print(a.count('ha')) # 0
8.字符串是否以某个成员开头或结尾
# coding:utf-8 if __name__ == '__main__': ''' string.startswith(item) 判断字符串是否以item开头 string.endswith(item) 判断字符串是否以item结尾 ''' a = 'abaacdef' print(a.startswith('ab')) # True print(a.endswith('ef')) # False
9.查找子串在主串中第一次出现的位置
# coding:utf-8 if __name__ == '__main__': ''' string.find(item) 和 string.index(item) 都是查找item在字符串中第一次出现的位置 string.find()找不到返回 -1 string.index() 找不到返回错误 所以使用string.find() 更多 ''' a = 'abcedefg' print(a.find('c')) # 2 print(a.index('c')) # 2 print(a.find('ac')) # -1
10.字符串过滤
# coding:utf-8 if __name__ == '__main__': ''' zstring.strip(char) 过滤字符。char为None则过滤等价空格的字符,否则过滤char字符,从左往右过滤一边,从右往左再过滤一遍 string.lstrip(char) 和strip() 相同,只不过该函数只从左往右过滤一遍 string.rstrip(char) 和strip() 相同,只不过该函数只从右往左过滤一遍 ''' a = ' abcdefgh ' b = ' abcdefgh ' c = 'aabbaa' print(a.strip()) print(b.strip()) print(c.strip('a')) # bb print(c.lstrip('a')) # bbaa print(c.rstrip('a')) # aabb
11.字符串替换
# coding:utf-8 if __name__ == '__main__': ''' string.replace(old,new,max) 将字符串中的old字符串替换成new字符串,替换max个,当max为None时替换全部 ''' a = 'abcdcdefg' print(a.replace('cd', 'ff')) # abffffefg print(a.replace('cd', 'ff', 1)) # abffcdefg print(a.replace('hh', 'ff')) # abcdcdefg
12.字符串的is函数
# coding:utf-8 if __name__ == '__main__': ''' string.isspace() 判断字符串是否仅仅由空格字符串构成 string.istitle() 判断所有单词的首字母是否大写 string.isupper() 判断所有英文字母是否大写 string.islower() 判断所有英文字母是否小写 ''' a = ' a ' space = ' ' title = 'I Love You' title2 = 'I Love you' upper = 'I LOVE YOU' upper2 = 'I LOVE You' lower = 'i love you' lower2 = 'I love you' print(space.isspace()) # True print(a.isspace()) # False print(title.istitle()) # True print(title2.istitle()) # False print(upper.isupper()) # True print(upper2.isupper()) # False print(lower.islower()) # True print(lower2.islower()) # False
13.字符串切片
# coding:utf-8 if __name__ == '__main__': a = 'abcdefg'
切片使用:
- 1.
list[start:end:step]
从索引start开始,每隔step获取一个元素,直至索引end(取到的值包含索引start上的值,不包含索引end上的值),返回一个新列表 - start默认值0,end默认值len(list),step默认值1 例1
- 2.当step为负数代表从右往左取 例2
- 3.当start或end为负数时,代表从右往左确认索引,最右边的从索引-1开始 例3
- 4.由于字符串不可更改,故此没有切片赋值
例1
print(a[::]) # abcdefg print(a[0:len(a):1]) # abcdefg
例2
print(a[::-1]) # gfedcba
例3
print(a[-2:-1:1]) # f
加载全部内容