代码
#str内部功能 name=' aK am il.L iu' age=18 num=-11 ab='#' ac=('1','2','3','4','5','6','7') print(dir(name)) print(name.capitalize())#capitalize(self):首字母大写 print(name.center(30,'#'))#center(self, width, fillchar=None): 内容居中,width:总长度;fillchar:空白处填充内容,默认无 print(name.count('i'))#count(self, sub, start=None, end=None):子序列个数 ''' decode(self, encoding=None, errors=None):解码 encode(self, encoding=None, errors=None):编码,针对unicode ''' print(name.endswith('u'))#endswith(self, suffix, start=None, end=None):是否以xxx结束 startswith(self, prefix, start=None, end=None):是否起始 print(name) print(name.expandtabs(3))#expandtabs(self, tabsize=None):将tab转换成空格,默认一个tab转换成8个空格 print(name.find('i'))#find(self, sub, start=None, end=None):寻找子序列位置,如果没找到,返回 –1 print(name.find('b')) #format(*args, **kwargs):字符串格式化,动态参数,讲函数式编程时细说 print(name.index('il'))#index(self, sub, start=None, end=None): 子序列位置,如果没找到,报错 print(name.isalnum())# ''' isalnum(self): 是否是字母和数字 isalpha(self):是否是字母 isdigit(self):是否是数字 islower(self): 是否小写 translate(self, table, deletechars=None):转换,需要先做一个对应表,最后一个表示删除字符集合 ''' print(ab.join(ac))#join(self, iterable):连接 print(name.ljust(30,'$'))#ljust(self, width, fillchar=None):内容左对齐,右侧填充 zfill(self, width):方法返回指定长度的字符串,原字符串右对齐,前面填充0。 print(name.lower())#lower(self):变小写 swapcase(self):大写变小写,小写变大写 print(name.lstrip())#lstrip(self, chars=None):移除左侧空白 strip(self, chars=None):移除两端空白 print(name.partition('m'))#partition(self, sep):分割,前,中,后三部分 print(name.replace('am', 'qqqqqq'))#replace(self, old, new, count=None):替换 print(name.split())#split(self, sep=None, maxsplit=None):分割, maxsplit最多分割几次 print(name.splitlines())#splitlines(self, keepends=False):根据换行分割
结果
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] ak am il.l iu ####### aK am il.L iu######## 2 True aK am il.L iu aK am il.L iu 8 -1 8 False 1#2#3#4#5#6#7 aK am il.L iu$$$$$$$$$$$$$$$ ak am il.l iu aK am il.L iu (' aK a', 'm', ' il.L iu') aK qqqqqq il.L iu ['aK', 'am', 'il.L', 'iu'] [' aK am il.L iu']