class str(object)
| str(object=’’) -> str
| str(bytes_or_buffer[, encoding[, errors]]) -> str
| Create a new string object from the given object. If encoding or
| errors is specified, then the object must expose a data buffer
| that will be decoded using the given encoding and error handler.
| Otherwise, returns the result of object.str() (if defined)
| or repr(object).
| encoding defaults to sys.getdefaultencoding().
| errors defaults to ‘strict’.
| Methods defined here:
| add(self, value, /)
| Return self+value.
| contains(self, key, /)
| Return key in self.
| eq(self, value, /)
| Return self==value.
| format(self, format_spec, /)
| Return a formatted version of the string as described by format_spec.
| ge(self, value, /)
| Return self>=value.
| getattribute(self, name, /)
| Return getattr(self, name).
| getitem(self, key, /)
| Return self[key].
| getnewargs(…)
| gt(self, value, /)
| Return self>value.
| hash(self, /)
| Return hash(self).
| iter(self, /)
| Implement iter(self).
| le(self, value, /)
| Return self<=value.
| len(self, /)
| Return len(self).
| lt(self, value, /)
| Return self<value.
| mod(self, value, /)
| Return self%value.
| mul(self, value, /)
| Return selfvalue.
| ne(self, value, /)
| Return self!=value.
| repr(self, /)
| Return repr(self).
| rmod(self, value, /)
| Return value%self.
| rmul(self, value, /)
| Return valueself.
| sizeof(self, /)
| Return the size of the string in memory, in bytes.
| str(self, /)
| Return str(self).
以下为字符串的自带函数
| capitalize(self, /)
| Return a capitalized version of the string.
| More specifically, make the first character have upper case and the rest lower case.
将字符串开头字母大写
>>> a = 'abc'
>>> a.capitalize()
'Abc'
| casefold(self, /)
| Return a version of the string suitable for caseless comparisons.
将字符串全变成小写
>>> a = 'ABcd'
>>> a.casefold()
'abcd'
| center(self, width, fillchar=’ ', /)
| Return a centered string of length width.
| Padding is done using the specified fill character (default is a space).
将字符串居中,fillchar传入需要填充的字符串,默认是空格
>>> a = 'ABcd'
>>> a.center(20,' ')
' ABcd '
>>> a.center(20,'+')
'++++++++ABcd++++++++'
| count(…)
| S.count(sub[, start[, end]]) -> int
| 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.
返回字符串在S里面出现的次数
>>> a = '1244'
>>> a.count('1')
1
>>> a.count('4')
2
| encode(self, /, encoding=‘utf-8’, errors=‘strict’)
| Encode the string using the codec registered for encoding.
| encoding
| The encoding in which to encode the string.
| errors
| The error handling scheme to use for encoding errors.
| The 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 can handle UnicodeEncodeErrors.
对字符串进行编码,encoding传入编码的格式,默认为utf—8,error,用于编码错误的处理方法,包括(‘ignore’, ‘replace’ ,'xmlcharrefreplace’以及在编码器中注册的任何可能的其他名称)
a = '1244'
>>> a.encode()
b'1244'
>>> a = '开开心心'
>>> a.encode()
b'\xe5\xbc\x80\xe5\xbc\x80\xe5\xbf\x83\xe5\xbf\x83'
| endswith(…)
| S.endswith(suffix[, start[, end]]) -> bool
| 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.
| suffix can also be a tuple of strings to try.
查看字符串的结尾是否是给定的字符串结尾,如果是返回True,如果不是则返回False
a = '开开