一、进制 1. 硬盘上保存数据: 01010101010101010101 2. 读取:01010101010101010101 -> 对应的编码的汉字 --> xx 3. 看到的: - 转换完成的字符串 - 以十六进制展示的01010101 4. 以二进制打开文件 f = open(‘log‘,‘rb‘) 二.数据类型 ####################################################### str ####################################################### ------------------------------------------------------------------------------------------------------------------------------- 1. capitalize(self) #字符串首字母大写 return "" # S.capitalize() -> str # Return a capitalized version of S, i.e. make the first character have upper case and the rest lower case. # 返回一个大写版本的 S,即第一个字符大写其余小写。 例: S = ‘alex‘ v = S.capitalize() print(v) #结果:Alex -------------------------------------------------------------------------------------------------------------------------------- 2. casefold(self) #将所有大小变小写,casefold更牛逼,可以转化德语等其他语言... return "" # S.casefold() -> str # Return a version of S suitable for caseless comparisons. # 返回一个比较适合的 S 小写版本。 例: S = ‘AleX‘ v = S.casefold() print(v) #结果:alex -------------------------------------------------------------------------------------------------------------------------------- 3. lower(self) #将所有大小变小写 return "" # S.lower() -> str # Return a copy of the string S converted to lowercase. # 返回已转换为小写字符串 S 的副本 例: S = ‘AleX‘ v = v = S.lower() print(v) #结果:alex ------------------------------------------------------------------------------------------------------------------------------- 4. center(self, width, fillchar=None) #文本居中 return "" # S.center(width[, fillchar]) -> str # Return S centered in a string of length width. Padding is done using the specified fill character (default is a space) # 返回 以S为中心 长度为width 的字符串。使用指定的填充字符 填充(默认是空格) # 参数1: 表示总长度 # 参数2:空白处填充的字符(长度为1) 例: S = ‘alex‘ v = S.center(10) n = S.center(10,‘行‘) a = S.center(2,‘行‘) b = S.center(5,‘行‘) print(v) #结果: alex print(n) #结果:行行行alex行行行 print(a) #结果:alex print(b) #结果:行alex ------------------------------------------------------------------------------------------------------------------------------- 5. count(self, sub, start=None, end=None) #表示传入值在字符串中出现的次数 return 0 #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. # 返回 sub 在字符串 S [start:end] 中 非重叠出现的子串次数。可选参数的 start和end 被解释为切片符号。 # 参数1: 要查找的值(子序列) # 参数2: 起始位置(索引) # 参数3: 结束位置(索引) 例: S = "alexasdfdsafsdfasdfaaaaa" v = S.count(‘a‘) n = S.count(‘df‘) a = S.count(‘df‘,12) b = S.count(‘df‘,0,15) print(v) #结果:9 print(n) #结果:3 print(a) #结果:2 print(b) #结果:2 ------------------------------------------------------------------------------------------------------------------------------- 6. endswith(self, suffix, start=None, end=None) #是否以xx结尾 return False # 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. # 如果字符串S的结尾和指定的suffix相同返回True,否则返回False。可选start,测试 在S的该位置开始。可选end,停止匹配 在S的该位置。suffix可以是一个元组去尝试。 # 参数1: 要查找的值(子序列) # 参数2: 起始位置(第一个字符为1) # 参数3: 结束位置(第一个字符为1) 例: S = ‘alex‘ v = S.endswith(‘ex‘) n = S.endswith(("w","x")) a = S.endswith(("w","e")) b = S.endswith(("w","a"),0,1) print(v) #结果:True print(n) #结果:True print(a) #结果:False print(b) #结果:True ------------------------------------------------------------------------------------------------------------------------------- 7. startswith(self, prefix, start=None, end=None) #是否以xx开始 return False # S.startswith(prefix[, start[, end]]) -> bool # Return True if S starts with the specified prefix, False otherwise. With optional 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. # 如果字符串S的开始和指定的suffix相同返回True,否则返回False。可选start,测试 在S的该位置开始。可选end,停止匹配 在S的该位置。suffix可以是一个元组去尝试。 # 参数1: 要查找的值(子序列) # 参数2: 起始位置(第一个字符为1) # 参数3: 结束位置(第一个字符为1) 例: S = ‘alex‘ v = S.startswith(‘al‘) n = S.startswith(("w","x")) a = S.startswith(("w","e")) b = S.startswith(("w","a"),0,1) print(v) #结果:True print(n) #结果:False print(a) #结果:True print(b) #结果:True ------------------------------------------------------------------------------------------------------------------------------- 8. def encode(self, encoding=‘utf-8‘, errors=‘strict‘) #转换成字节 return b"" # S.encode(encoding=‘utf-8‘, errors=‘strict‘) -> bytes # Encode S using the codec registered for encoding. Default encoding is ‘utf-8‘. 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 can handle UnicodeEncodeErrors. # 例: S = "李杰" v1 = S.encode(encoding=‘utf-8‘) # 字节类型 print(v1) # 结果:b‘\xe6\x9d\x8e\xe6\x9d\xb0‘ v2 = S.encode(encoding=‘gbk‘) # 字节类型 print(v2) # 结果:b‘\xc0\xee\xbd\xdc‘ ------------------------------------------------------------------------------------------------------------------------------- 9. expandtabs(self, tabsize=8) #找到制表符\t,进行替换(包含前面的值) return "" # S.expandtabs(tabsize=8) -> str # 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. # 返回使用空格扩展所有tab字符的S的副本。如果没有给出tabsize,一个tab制表符大小 假设为8个字符。 例: S = "al\te\tx\nalex\tuu\tkkk" v = S.expandtabs(10) print(v) 结果: al e x alex uu kkk ------------------------------------------------------------------------------------------------------------------------------- 10. find(self, sub, start=None, end=None) #找到指定子序列的索引位置:不存在返回-1 return 0 # 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. # 返回 在最小的索引 开始 ,字符串 S 中发现子串sub,这样的 sub 是包含在 S[start:end]里。可选参数start和end被解释为切片符号。失败返回-1。 例: S = ‘alex‘ v = S.find(‘e‘,0,1) print(v) #结果:-1 ------------------------------------------------------------------------------------------------------------------------------- 11. index(self, sub, start=None, end=None) #找到指定子序列的索引位置 return 0 # S.index(sub[, start[, end]]) -> int # Like S.find() but raise ValueError when the substring is not found. # 像S.find() 但是 当在字符串中没有找到时 报错。 例: S = ‘alex‘ v = S.index(‘e‘,0,1) print(v) #结果:报错ValueError: substring not found ------------------------------------------------------------------------------------------------------------------------------- 12. format(*args, **kwargs) #字符串格式化 pass # S.format(*args, **kwargs) -> str # Return a formatted version of S, using substitutions from args and kwargs. The substitutions are identified by braces (‘{‘ and ‘}‘). # 返回一个格式化的版本 S,使用args 和 kwargs替换。替换是以括号(‘{‘和‘}‘)鉴别。 例: S = "我是:{0};年龄:{1};性别:{2}" v = S.format("李杰",19,‘都行‘) print(v) #结果:我是:李杰;年龄:19;性别:都行 S = "我是:{name};年龄:{age};性别:{gender}" v = S.format(name=‘李杰‘,age=19,gender=‘随意‘) print(v) #结果:我是:李杰;年龄:19;性别:都行 ------------------------------------------------------------------------------------------------------------------------------- 13. format_map(self, mapping) #字符串格式化 return "" # S.format_map(mapping) -> str # Return a formatted version of S, using substitutions from mapping. The substitutions are identified by braces (‘{‘ and ‘}‘). # 返回一个格式化的版本 S,使用mapping替换。替换是以括号(‘{‘和‘}‘)鉴别。 例: S = "我是:{name};年龄:{age};性别:{gender}" v = S.format_map({‘name‘:"李杰",‘age‘:19,‘gender‘:‘中‘}) print(v) #结果:我是:李杰;年龄:19;性别:中 ------------------------------------------------------------------------------------------------------------------------------- 14. isalnum(self) #是否是数字、汉字、数字. return False # S.isalnum() -> bool # Return True if all characters in S are alphanumeric and there is at least one character in S, False otherwise. # 如果S中的所有字符都是字母数字,则至少有一个字符在S中返回True,否则为False。 例: S = ‘alex8汉子‘ v = S.isalnum() print(v) #结果:True ------------------------------------------------------------------------------------------------------------------------------- 15. isalpha(self) #是否是字母、汉字. return False # S.isalpha() -> bool # Return True if all characters in S are alphabetic and there is at least one character in S, False otherwise. # 如果S中的所有字符都是字母,则至少有一个字符在S中返回Ture,否则为False。 例: S = ‘alex8汉子‘ v = S.isalpha() print(v) #结果:False ------------------------------------------------------------------------------------------------------------------------------- 16. isdecimal(self) #是否是阿拉伯数字 return False # S.isdecimal() -> bool # Return True if there are only decimal characters in S, False otherwise. # 如果只有十进制数的字符则返回Ture 否则False 例: S = ‘二‘ v = S.isdecimal() # ‘123‘ print(v) #结果:False ------------------------------------------------------------------------------------------------------------------------------- 17. isdigit(self) #是否是数字 return False # S.isdigit() -> bool # Return True if all characters in S are digits and there is at least one character in S, False otherwise. # 如果S中的所有字符都是数字,并且至少有一个字符在s中返回True,否则为False。 例: S = ‘二‘ v = S.isdigit() # ‘123‘,‘②‘ print(v) #结果:False ------------------------------------------------------------------------------------------------------------------------------- 18. isnumeric(self) #是否是数字字符 return False # S.isnumeric() -> bool # Return True if there are only numeric characters in S, False otherwise. # 如果S中只有数字字符,则返回True,否则为False。 例: S = ‘二‘ v = S.isnumeric() # ‘123‘,‘二‘,‘②‘ print(v) #结果:Ture ------------------------------------------------------------------------------------------------------------------------------- 19. isidentifier(self) #是否是表示符 return False # S.isidentifier() -> bool # Return True if S is a valid identifier according to the language definition. Use keyword.iskeyword() to test for reserved identifiers such as "def" and "class". # 如果S是一个有效标识符,则根据语言定义返回true。使用关键词。iskeyword()测试保留标识符如“def”和“class”。 例: S = ‘name‘ v = S.isidentifier() print(v) #结果:Ture ------------------------------------------------------------------------------------------------------------------------------- 20. islower(self) #是否全部是小写 return False # S.islower() -> bool # Return True if all cased characters in S are lowercase and there is at least one cased character in S, False otherwise. # 如果S中的所有字符都是小写字母,并且在S中至少有一个字符返回true,否则为False 例: S = "ALEX" v = S.islower() print(v) #结果:False ------------------------------------------------------------------------------------------------------------------------------- 21. isupper(self) #是否全部是大写 return False # S.isupper() -> bool # Return True if all cased characters in S are uppercase and there is at least one cased character in S, False otherwise. # 如果S中的所有字符都是大写,并且至少包装一个字符在S里返回Ture,否则为False 例: S = "ALEX" v = S.isupper() print(v) #结果:True ------------------------------------------------------------------------------------------------------------------------------- 22. isprintable(self) #是否包含隐含的xx return False # S.isprintable() -> bool # Return True if all characters in S are considered printable in repr() or S is empty, False otherwise. # 如果所有的字符是repr()标准打印或是空返回true,否则为false。 例: S = "钓鱼要钓刀鱼,\t刀鱼要到岛上钓" v = S.isprintable() print(v) #结果:False ------------------------------------------------------------------------------------------------------------------------------- 23. isspace(self) #是否全部是空格 return False # S.isspace() -> bool # Return True if all characters in S are whitespace and there is at least one character in S, False otherwise. # 如果所有字符在S中是空格 并且至少有一个字符在S里 ,返回true,否则为False 。 例: S = ‘ ‘ v = S.isspace() print(v) #结果:Ture ------------------------------------------------------------------------------------------------------------------------------- 24.☆☆☆☆☆ join(self, iterable) # 元素拼接(元素字符串) return "" # S.join(iterable) -> str # Return a string which is the concatenation of the strings in the iterable. The separator between elements is S. # 返回一个连接每个字符的iterable。元素之间的分隔符是 S. 例: iterable = ‘alex‘ S = "_" v = S.join(iterable) # 内部循环每个元素 print(v) # 结果:a_l_e_x
iterable = [‘海峰‘,‘杠娘‘,‘李杰‘,‘李泉‘] S = "搞" v = S.join(iterable) print(v) # 结果:海峰搞杠娘搞李杰搞李泉 ------------------------------------------------------------------------------------------------------------------------------- 25. ljust(self, width, fillchar=None) # 左填充 return "" # S.ljust(width[, fillchar]) -> str # Return S left-justified in a Unicode string of length width. Padding is done using the specified fill character (default is a space). # 返回左对齐的S ,Unicode字符串的长度为width。填充使用指定的填充字符(默认是空格)。 例: S = ‘alex‘ v = S.ljust(10,‘*‘) print(v) #结果:alex****** ------------------------------------------------------------------------------------------------------------------------------- 26. rjust(self, width, fillchar=None) #右填充 return "" # S.rjust(width[, fillchar]) -> str # Return S right-justified in a string of length width. Padding is done using the specified fill character (default is a space). # 返回右对齐的S ,Unicode字符串的长度为width。填充使用指定的填充字符(默认是空格)。 例: S = ‘alex‘ v = S.rjust(10,‘*‘) print(v) #结果:******alex ------------------------------------------------------------------------------------------------------------------------------- 27. maketrans(self, *args, **kwargs) #对应关系 + 翻译 pass # Return a translation table usable for str.translate(). # If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters to Unicode ordinals, strings or None. Character keys will be then converted to ordinals. If there are two arguments, they must be strings of equal length, and in the resulting dictionary, each character in x will be mapped to the character at the same position in y. If there is a third argument, it must be a string, whose characters will be mapped to None in the result.
translate(self, table) return "" # S.translate(table) -> str # Return a copy of the string S in which each character has been mapped through the given translation table. The table must implement lookup/indexing via __getitem__, for instance a dictionary or list, mapping Unicode ordinals to Unicode ordinals, strings, or None. If this operation raises LookupError, the character is left untouched. Characters mapped to None are deleted.
例: m = str.maketrans(‘aeiou‘,‘12345‘) # 对应关系 S = "akpsojfasdufasdlkfj8ausdfakjsdfl;kjer09asdf" v = S.translate(m) print(v) #结果:1kps4jf1sd5f1sdlkfj815sdf1kjsdfl;kj2r091sdf ------------------------------------------------------------------------------------------------------------------------------- 28. partition(self, sep) #分割,保留分割的元素 pass # S.partition(sep) -> (head, sep, tail) # Search for the separator sep in S, and return the part before it, the separator itself, and the part after it. If the separator is not found, return S and two empty strings. # 在S中搜索分离器SEP,并返回它之前的部分、分离器本身和它后面的部分。如果找不到分隔符,则返回S和两个空字符串。 例: S = "李泉SB刘康SB刘一" v = S.partition(‘SB‘) print(v) #结果:(‘李泉‘, ‘SB‘, ‘刘康SB刘一‘) ------------------------------------------------------------------------------------------------------------------------------- 29. replace(self, old, new, count=None) return "" # S.replace(old, new[, count]) -> str # Return a copy of S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced. # 返回一份用new 取代 S所有的old字符串。如果给定可选参数计数,则只有前 count 个事件被替换。 例: S = "李泉SB刘康SB刘浩SB刘一" v = S.replace(‘SB‘,‘Love‘) print(v) #结果:李泉Love刘康Love刘浩Love刘一 v = S.replace(‘SB‘,‘Love‘,1) print(v) #结果:李泉Love刘康SB刘浩SB刘一 ------------------------------------------------------------------------------------------------------------------------------- 30. rfind(self, sub, start=None, end=None) return 0 # 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. # 返回 在最大的索引开始 ,字符串 S 中发现子串sub ,这样的 sub 是包含在 S[start:end]里。可选参数start和end被解释为切片符号。失败返回-1。 例: S = ‘alex‘ v = S.rfind(‘e‘,2,3) print(v) #结果:2 ------------------------------------------------------------------------------------------------------------------------------- 31. rindex(self, sub, start=None, end=None) return 0 # S.rindex(sub[, start[, end]]) -> int # Like S.rfind() but raise ValueError when the substring is not found. # 像S.rfind() 但是当没有找到字符串时 报ValueError 例: S = ‘alex‘ v = S.rindex(‘e‘,2,3) print(v) #结果:2 ------------------------------------------------------------------------------------------------------------------------------- 32. rpartition(self, sep) pass # S.rpartition(sep) -> (head, sep, tail) # Search for the separator sep in S, starting at the end of S, and return the part before it, the separator itself, and the part after it. If the separator is not found, return two empty strings and S. # 例: S = "李泉SB刘康SB刘一" v = S.rpartition(‘SB‘) print(v) #结果:(‘李泉SB刘康‘, ‘SB‘, ‘刘一‘) ------------------------------------------------------------------------------------------------------------------------------- 33. rsplit(self, sep=None, maxsplit=-1) return [] # S.rsplit(sep=None, maxsplit=-1) -> list of strings # Return a list of the words in S, using sep as the delimiter string, starting at the end of the string and working to the front. If maxsplit is given, at most maxsplit splits are done. If sep is not specified, any whitespace string is a separator. # 返回一个列表,单词在S中,使用sep作为分隔符,从字符串的结尾开始并工作到最前面。如果maxsplit是给定的,最多分裂maxsplit次。如果未指定任何SEP,空格的字符串是一个分离器。 例: S = ‘as|ww|ee‘ v = S.rsplit(‘|‘,1) print(v) #结果:[‘as|ww‘, ‘ee‘] ------------------------------------------------------------------------------------------------------------------------------- 34. rstrip(self, chars=None) #去除尾部空格 return "" # S.rstrip([chars]) -> str # Return a copy of the string S with trailing whitespace removed. If chars is given and not None, remove characters in chars instead. # 返回一个拷贝的字符串的尾部空格删除。如果字符是给定的,而不是没有,删除字符用chars代替。 例: S = ‘ALEX ‘ v = S.rstrip() print(v) #结果:ALEX ------------------------------------------------------------------------------------------------------------------------------- 35. lstrip(self, chars=None) return "" # S.lstrip([chars]) -> str # Return a copy of the string S with leading whitespace removed. If chars is given and not None, remove characters in chars instead. #返回一个拷贝的字符串的开头空格删除。如果字符是给定的,而不是没有,删除字符用chars代替。 例: S = ‘ ALEX‘ v = S.lstrip() print(v) #结果:ALEX ------------------------------------------------------------------------------------------------------------------------------- 36. split(self, sep=None, maxsplit=-1) # return [] # S.split(sep=None, maxsplit=-1) -> list of strings # Return a list of the words in S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result. # 返回一个列表,单词在S中,用SEP作为分隔符串。如果maxsplit是给定的,最多分裂maxsplit次。如果sep未被指定或者是空,任何空白字符是一个分离器并且空白字符在结果中移除。 例: S = ‘as|ww|ee‘ v = S.split(‘|‘,1) print(v) #结果:[‘as‘, ‘ww|ee‘] ------------------------------------------------------------------------------------------------------------------------------- 37. splitlines(self, keepends=None) #按回车生产列表 return [] # S.splitlines([keepends]) -> list of strings # Return a list of the lines in S, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true. # 返回一个列表,在S中的行,在行边界处断开。换行不包含在结果列表中,除非keepends给出并为Ture。 例: S = """ALEX asd """ v = S.splitlines() print(v) #结果:[‘ALEX‘, ‘asd‘] ------------------------------------------------------------------------------------------------------------------------------- 38. strip(self, chars=None) 移除空白,\n,\t return "" # S.strip([chars]) -> str # Return a copy of the string S with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead. # 返回一个拷贝的字符串的开头和结尾的空格去掉。如果字符是给定的,而不是没有,删除字符用chars代替。 例: S = ‘ ALEX ‘ v = S.strip() print(v) #结果:ALEX ------------------------------------------------------------------------------------------------------------------------------- 39. swapcase(self) # 大小写转换 return "" # S.swapcase() -> str # Return a copy of S with uppercase characters converted to lowercase and vice versa. # 返回一个s的拷贝,大写字符转换为小写,反之亦然。 例: S = "Alex" v = S.swapcase() print(v) #结果:aLEX ------------------------------------------------------------------------------------------------------------------------------- 40. title(self) #转换 标题版 return "" # S.title() -> str # Return a titlecased version of S, i.e. words start with title case characters, all remaining cased characters have lower case. # 返回一个titlecased版的S,从单词开始标题版 字符串,所有剩余的字符的小写。 例: S = "i have a dream" v =S.title() print(v) #结果:I Have A Dream ------------------------------------------------------------------------------------------------------------------------------- 41. istitle(self) #是否是标题版 return False # S.istitle() -> bool # Return True if S is a titlecased string and there is at least one character in S, i.e. upper- and titlecase characters may only follow uncased characters and lowercase characters only cased ones. Return False otherwise. # 如果S是一个titlecased字符串并且至少有一个字符在S里返回True,i.e.大写 并且 titlecase字符只能跟着未包装的字符和小写字符的唯一情况。否则返回False。 例: S = "I Have A Dream" v = S.istitle() print(v) #结果:Ture ------------------------------------------------------------------------------------------------------------------------------- 42. upper(self) #转换大写 return "" # S.upper() -> str # Return a copy of S converted to uppercase. # 返回一个S 的大写副本 例: S = ‘Alex‘ v = S.upper() print(v) #结果:ALEX ------------------------------------------------------------------------------------------------------------------------------- 43. zfill(self, width) #填充0 return "" # S.zfill(width) -> str # Pad a numeric string S with zeros on the left, to fill a field of the specified width. The string S is never truncated. # 在左边填充一个带零的数字字符串,以填充指定width的字段。字符串从不被截断。 例: S = "alex" v = S.zfill(10) print(v) #结果:000000alex -------------------------------------------------------------------------------------------------------------------------------