Python基礎學習 - 容器之字串1
阿新 • • 發佈:2020-12-27
字串(string)
字串通常使用一對單引號/雙引號來定義。
字串內建方法
# dir() 不帶引數時,返回當前範圍內的變數、方法和定義的型別列表
# dir() 帶引數時,返回引數的屬性、方法列表
dir(str)
# help() 幫助具體理解
help(str)
含義 |
---|
__add__(self, value, /) Return self+value 新增元素 |
'_ _ class _ _ ' 空 |
__contains__(self, key, /) Return key in self 判斷是否包含某字串,包含則返回True |
'_ _ delattr _ _ ' |
'_ _ dir _ _ 檢視一些方法等 |
'_ _ doc _ _ ' 空 |
__eq__(self, value, /) Return self==value 判斷是否相同,相同則返回True |
__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 |