Python 字符串操作

来源:互联网 发布:搜题软件 编辑:程序博客网 时间:2024/06/06 15:50

这里是我自己学习当中总结出来的,想做个笔记,仅供参考!

就是字符串的所有属性我做了个列表;
定义:一个字符串 s = 'abcdefABCDEF'


                            Python 字符串操作 参考表
属性名 用法 结果 官方说明(en) —-个人理解(china) 相应博客 capitalize() print(s.capitalize()) Abcdefabcdef Return a copy of the string S with only its first character capitalized. 把串中的首字母变大写其它全变为小写 查看博客 center() print(s.center(23)) ____abcdefABCDEF Return S centered in a string of length width. Padding is done using the specified fill character (default is a space) 如果你写的数字大于字符串的长度则在前面加空格(下划线代替) 查看博客 count() print(s.count(‘f’)) –count–: 1 Return the number of non-overlapping occurrences of substring sub in string S[start:end]. Optional arguments start and end are interpreted as in slice notation. 返回子串出现的各数 int类型的,从字符串的开始到结束(区分大小写)。 None decode() s.decode(‘utf-8’) abcdefABCDEF Decodes S using the codec registered for encoding. encoding defaults to the default encoding. errors may be given to set a different error handling scheme. Default is ‘strict’ meaning that encoding errors raise UnicodeDecodeError. Other possible values are ‘ignore’ and ‘replace’ as well as any other name registered with codecs.register_error that is able to handle UnicodeDecodeErrors. 编码格式,默认使用当前系统的编码 None encode() s.encode(‘utf-8’) abcdefABCDEF Encodes S using the codec registered for encoding. encoding defaults to the default encoding. errors may be given to set a different error handling scheme. Default is ‘strict’ meaning that encoding errors raise a UnicodeEncodeError. Other possible values are ‘ignore’, ‘replace’ and ‘xmlcharrefreplace’ as well as any other name registered with codecs.register_error that is able to handle UnicodeEncodeErrors. None None endswith() print(s.endswith(‘F’)) True Return True if S ends with the specified suffix, False otherwise.With optional start, test S beginning at that position. With optional end, stop comparing S at that position. can also be a tuple of strings to try. 以指定的后缀结束 返回True,则False None expandtabs() s.expandtabs() abcdefABCDEF Return a copy of S where all tab characters are expanded using spaces. If tabsize is not given, a tab size of 8 characters is assumed. None None find() print(‘—- find:%d’ % s.find(‘f’))# —- find:5 Return the lowest index in S where substring sub is found,such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation. None None format() Return a formatted version of S, using substitutions from args and kwargs.The substitutions are identified by braces (‘{’ and ‘}’). None None
                                                                    ————制表人:Terry

其它部分还在持续更新中····;

0 0