Python基础学习 - 容器之字符串1

字符串(string)

字符串通常使用一对单引号/双引号来定义。

字符串内置方法

# dir() 不带参数时,返回当前范围内的变量、方法和定义的类型列表
# dir() 带参数时,返回参数的属性、方法列表
dir(str)
# help() 帮助具体理解
help(str)
含义
__add__(self, value, /)
Return self+value
添加元素
str.add()
'_ _ class _ _ '
__contains__(self, key, /)
Return key in self
判断是否包含某字符串,包含则返回True
]
'_ _ delattr _ _ '
'_ _ dir _ _
查看一些方法等
空
'_ _ doc _ _ '
__eq__(self, value, /)
Return self==value
判断是否相同,相同则返回True
str.add()
__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)
'_ _ init _ _ '
初始化
'_ _ init_subclass _ _ '
__iter__(self, /)
Return 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 self*value
__ne__(self, value, /)
Return self!=value
'_ _ new _ _ '
'_ _ reduce _ _ '
'_ _ reduce_ex _ _ '
__repr__(self, /)
Return repr(self)
__rmod__(self, value, /)
Return value%self
__rmul__(self, value, /)
Return value*self
'_ _ setattr _ _ '
__sizeof__(self, /)
Return the size of the string in memory, in bytes
__str__(self, /)
Return str(self)
返回本身
在这里插入图片描述
'_ _ subclasshook _ _ '
capitalize(self, /)
Return a capitalized version of the string
More specifically, make the first character have upper case and the rest lower case首字母大写
casefold(self, /)
Return a version of the string suitable for caseless comparisons首字母小写
在这里插入图片描述
center(self, width, fillchar=' ', /)
Return a centerd string of length windth
Padding is done using the specified fill character (default is a space)指定长度和填充字符,内容居中,填充字符留空则为空格
在这里插入图片描述
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计数
在这里插入图片描述
encode(self, /, encoding='utf-8', errors='strict')
Encoding 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编码
在这里插入图片描述
endswith(...)
S.endswith(suffix[, start[, end]]) -> bool
Return True if S ends with the specified suffix, False otherwise. With optinal 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
下一篇
expandtabs(self, /, tabsize=8)
Return a copy where all tab characters are expanded using spaces. If tabsize is not given, a tab size of 8 characters is assumed
把1个tab键转换为7个空格
在这里插入图片描述
find(...)
S.find(sub[, start[, end]]) -> int
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. Return -1 on failure
下一篇
format(...)
S.format(*args, **kwargs) -> str
Return a formatted version of S, using substitutions from args and kwargs. The substitutions are identified by braces ('{' and '}').
下一篇
format_map(...)
S.format_map(mapping) -> str
Return a formatted version of S, using substitutions from mapping. The substitutions sre identified by braces ('{' and '}")
index(...)
S.index(sub[, start[, end]]) -> int
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. Raises ValueError when the substring is not found
下一篇
isalnum(self, /)
Return True if the string is an alpha-numeric string, False otherwise. A string is alpha-numeric if all characters in the string are alpha-numeric and there is at least one character in the string判断是否全部是字母/数字
在这里插入图片描述
isalpha(self, /)
Return True if the string is an alphabetic string, False otherwise. A string is alphabetic if all characters in the string are alphabetic and there is at least one character in the string判断是否全部是字母
在这里插入图片描述
isascii(self, /)
Return True if all characters in the string are ASCII, False otherwise. ASCII characters have code points in the range U+0000-U+007F. Empty string is ASCII too.
isdecimal(self, /)
Return True if the string is a decimal string, False otherwise. A string is decimal string if all characters in the string are decimal and there is at least one character in the string.判断是否全部为十进制小数
isdigit(self, /)
Return True if the string is a digit string, False otherwise. A string is digit string if all characters in the string are digits and there is at least one character in the string.判断是否全部为数字
isidentifier(self, /)
Return True if the string is a valid Python identifier, False otherwise. Use keyword.iskeyword() to test for reserved identifiers such as "def" and "class"
判断是否全部为标识符/变量名(不能以数字开头)
空
islower(self, /)
Return True if the string is a lowercase string, False otherwise. A string is lowercase if all cased characters in the string are lowercase and there is at least one cased character in the string判断是否全部为小写
在这里插入图片描述
isnumeric(self, /)
Return True if the string is a numeric string, False otherwise. A string is numeric if all characters in the string are numeric and there is at least one character in the string.判断是否全部为数字
isprintable(self, /)
Return True if the string is printable, False otherwise. A string is printable if all of its characters are considered printable in repr() or if it is empty
判断是否为可打印字符/能否原样输出
空
isspace(self, /)
Return True if the string is a whitespace string, False otherwise. A string is whitespace if all characters in the string are whiteespace and there is at least one character in the string.
判断是否全部是空格
istitle(self, /)
Return True if the string is a title-cased string, False otherwise. In a title-cased string, upper- and title-case characters may only follow uncased characters and lowercase characters only cased ones
判断是否全部是标题
空
isupper(self, /)
Return True if the string is an uppercase string, False otherwise. A string is uppercase if all cased characters in the string are uppercase and there is at least one character in the string.判断是否全部是大写
在这里插入图片描述
join(self, iterable, /)
Concatenate any number of strings. The string whose method is called is inserted in betweem each given string. The result is returned as a new string. Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
下一篇
ljust(self, width, fillchar=' ', /)
Return a left-justified string of length width. Padding is done using the specified fill character (default is a space)
指定长度和填充字符,内容左对齐,填充为空则为空格
空
lower(self, /)
Return a copy of the string converted to lowercase
全部换成小写
哈哈哈哈
lstrip(self, chars=None, /)
Return a copy of the string with leading whitespace removed. If chars is given and not None, remove characters in chars instead
移除字符串左侧指定的字符,默认为空格
空
'maketrans’
创建字符映射的转换表,配合translate函数使用
空
partition(self, sep, /)
Partition the string into three parts using the given separator. This will search for the separator in the string. If the separator is found, returns a 3-tuple containing the part before the separator, the separator itself, and the part after it. If the separator is not found, returns a 3-tuple containing the original string and two empty strings
指定分隔符,将字符串进行分割
下一篇
replace(self, old, new, count=1, /)
Return a copy with all occurrences of substring old replaced by new. count Maximum number of occurrences to replace. -1(the default value) means replace all occurrences. If the optional argument count is given, only the first count occurrences are replaced
替换
下一篇
rfind(...)
S.rfind(sub[, start[, end]]) -> int
Return the highest 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. Return -1 on failure.
从右查找指定字符串出现的位置,如果没有匹配项则返回-1
rindex(...)
S.rindex(sub[, start[, end]]) -> int
Return the highest 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. Return ValueError when the substring is not found .
从右往左找
下一篇
rjust(self, width, fillchar=' ', /)
Return a right-justified string of length width. Padding is done using the specified fill character (default is a space)
右对齐
rpartition(self, sep, /)
Partition the string into three parts using the given separator. This will search for the separator in the string, starting at the end. If the separator is found, returns a 3-tuple containing the part before the separator, the separator itself, and the part after it. If the separator is not found, returns a 3-tuple containing two empty strings and the original string
指定分隔符,从右边开始将字符串进行分割
rsplit(self, /, sep=None, maxsplit=-1)
Return a list of the words in the string, using sep as the delimiter string. sep The delimiter according which to split the string. None (the default value) means split according to any whitespace, and discard empty strings from the result. maxsplit Maximum number of spits to do. -1(the default value) means no limit. Splits are done starting at the end of the string and working to the front.
指定分隔符对字符串进行切片
下一篇
rstrip(self, chars=None, /)
Return a copy of the string with trailing whitespace removed. If chars is given and not None, remove characters in chars instead.
删除字符串末尾的指定字符,默认为空格
split(self, /, sep=None, maxsplit=-1)
Return a list of the words in the string, using sep as the delimiter string. sep The delimiter according which to split the string. None (the default value) means split according to any whitespace, and discard empty strings from the result. maxsplit Maximum number of spits to do. -1(the default value) means no limit
从左往右切分
splitlines(self, /, keepends=False)
Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is givem and true
根据换行符切分,返回一个列表
startswith(...)
S.startswith(prefix[, start[, end]]) -> bool
Return True if S starts with the specified prefix, False otherwise. With optinal start, test S beginning at that position. With optional end, stop comparing S at that position. prefix can also be a tuple of strings to try
判断开始
下一篇
strip(self, chars=None, /)
Return a copy of the string with leading and trailing whitespace remove. If chars is given and not None, remove characters in chars instead.
删除字符串前后的指定字符,默认为空格
swapcase(self, /)
Convert uppercase characters to lowercase and lowercase characters to uppercase.
大小写互换
title(self, /)
Return a version of the string where each word is titlecased. More specifically, words start with uppercased characters and all remaining cased characters have lower case
转换为标题,即每个单词首字母大写
translate(self, table, /)
Return each character in the string using the given translation table. table Translation table, which must be a mapping of Unicode ordinals to Unicode ordinals, strings, or None. The table must implement lookup/indexing via __getitem__, for instance a dictionary or list. If this operation raises LookupError, the character is left untouched. Characters mapped to None are deleted.
根据maketrans方法创建的表,进行字符替换
参照maketrans
upper(self, /)
Return a copy of the string converted to uppercase
小写转换成大写
空
zfill(self, width, /)
Pad a numeric string with zeros on the left, to fill a field of the given width. The string is never truncated.
指定字符串的长度,原字符串右对齐,前面填充0
空
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值