ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
>string内置方法和属性 #-*- encoding: utf-8 -*-; import string print dir(string) ['Formatter', 'Template', '_TemplateMetaclass', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_float', '_idmap', '_idmapL', '_int', '_long', '_multimap', '_re', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'atof', 'atof_error', 'atoi', 'atoi_error', 'atol', 'atol_error', 'capitalize', 'capwords', 'center', 'count', 'digits', 'expandtabs', 'find', 'hexdigits', 'index', 'index_error', 'join', 'joinfields', 'letters', 'ljust', 'lower', 'lowercase', 'lstrip', 'maketrans', 'octdigits', 'printable', 'punctuation', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitfields', 'strip', 'swapcase', 'translate', 'upper', 'uppercase', 'whitespace', 'zfill'] >find(str) : 返回index,不存在返回-1 text = 'libai' print text.find('i') # 1; >index(str) : 同find,不存在则报错 text = 'libai'; print text.index('j'); >split(seg) : 分割,返回list列表 text = 'libai'; print text.split('b') # ['li','ai'] >endswith(str) : 是否以str结尾 text = 'libai'; print text.endswith('ai') #True >startswith(str) : 是否以str开头 text = 'libai'; print text.startswith('li') #True >count(str) : str出现次数 text = 'libai'; print text.count('i') #2 >replace(str,target) : target替换str;全局替换 text = 'libai'; print text.replace('i','e') >join(segs) : 将segs链接起来,返回字符串 text = '-'; segs = ['libai','tangchao','shisheng']; print text.join(segs); >upper() : 大写 text = 'libai'; print text.upper(text) >lower() : 小写 text = 'libai'; text1= text.upper(); print text1.lower(); >capitalize() : 首字母大写 text = 'libai'; print text.capitalize() >其他函数 max(): 返回字符串最大的字符 min() : 返回最小的字符 title() : 每个单词首字母大写