最近有很多读者朋友在python中str使用方法有疑问。有网友整理了相关内容,希望能回答你的疑惑,关于pythonstr本网站还为您找到了问题的答案,希望对您有所帮助。
Python渗透学习(str字符串)
感谢您的关注,小编,我今天又要挖个坑,分享Python的学习笔记。
都说网络安全这条路,python必须学习语言。为什么呢?因为一旦某个漏洞在网上爆发,你必须用Python来编写EXP。这样可以避免不必要的人力,直接脚本检测就可以了。如果你对此没有详细的概念,请阅读我之前的redis获取服务器权限的文章。只要操作脚本,就可以直接代替人省略十几步操作,直接获得权限。方便吗?
从现在开始,我将不断分享我的Python学习笔记,供大家学习。
由于篇幅字数有限,没有办法把所有的笔记都放在这里。有兴趣的读者可以私下谈论我或加入我:92823686收到我所有的笔记。
下图是我想不断分享的Python模块笔记,附有源码解释、中文翻译和各种实例,让您对该模块有更深入的了解。
# capitalize() 第一个字母大写‘’capitalize(…) S.capitalize() -> str Return a capitalized version of S, i.e. make the first character have upper case and the rest lower case.”’#print(help(str.capitalize))a=’asdffghjkl’b=a.capitalize()print(b) # b=Asdffghjkl——————————————————————————————-# casefold() 返回小写,与lower()相似,但lower只针对ASCII,casefold()针对Unicode()”’casefold(…) S.casefold() -> str Return a version of S suitable for caseless comparisons.”’print(help(str.casefold))a=’ASD’b=a.casefold()print(b) # b=asd——————————————————————————————-# 用指定的宽度返回居中版s,# 如有必要,用filchar填充,默认为空格。但是s不会被截取。但是s不会被截取。# 也就是说,如果s的长度大于width,则不会截取s。”’center(…) 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)”’print(help(str.center))a=’asdfghjkl’b=a.center(20, ‘-‘) # 长度为20,a字符串居中,用数字5填充左右print(b) # b=55555asdfghjkl555555——————————————————————————————-”’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.”’print(help(str.count))a=’askdfksghjkl’b=a.count(‘k’) # 从下标为1到5计数s’的个数print(b) # b=2——————————————————————————————-# 以 encoding 指定的编码格式编码字符串。errors参数可以指定不同的错误处理方案。”’encode(…) 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.”’print(help(str.encode))a=’asdfsghjkl’b=a.encode(‘utf-8′)print(b) # b=b’asdfsghjkl’——————————————————————————————-# 判断字符串是否以指定的后缀结束,如果在指定的后缀结束时返回True,否则返回False。# 可选参数”start”与”end“检索字符串的开始和结束。# 可选参数”start”与”end”为了检索字符串的开始和结束。””endswith(…) S.endswith(suffix[, start[, end]]) -> bool suffix — 参数可以是字符串或元素。 start — 字符串中的开始位置。 end — 结束位置在字符中。 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.”’print(help(str.endswith))a=’asdfsghjk’b=a.endswith(‘k’)c=a.endswith(‘j’)d=a.endswith(‘a’, 0, 1)e=a.endswith(‘a’, 0, 2)print(b) # b=Trueprint(c) # c=Falseprint(d) # d=Trueprint(e) # e=False——————————————————————————————-# 判断字符串是否以指定的后缀结束,如果在指定的后缀结束时返回True,否则返回False。# 可选参数”start”与”end“检索字符串的开始和结束。”’expandtabs(…) 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.”’print(help(str.expandtabs))a=’asdfsghjk’b=a.endswith(‘k’)c=a.endswith(‘j’)d=a.endswith(‘a’, 0, 1)e=a.endswith(‘a’, 0, 2)print(b) # b=Trueprint(c) # c=Falseprint(d) # d=Trueprint(e) # e=False——————————————————————————————-”’find(…) 检测字符串是否包含子字符串 str , 如果指定 beg(开始)和 end(结束)范围,检查是否包含在指定范围内, 如果包含子字符串,则返回初始索引值,否则返回-1。 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.”’print(help(str.find))a=’asdfsghjk’b=a.find(‘d’)c=a.find(‘f’, 1, 4) #d=a.find(‘f’, 1, 3)print(b) # 2 下标print(c) # 2 从第一个字符到第三个字符,如果找到了,返回下标print(d) # -1——————————————————————————————-”’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 ‘}’).None”’print(help(str.format))name=’ske’fruit=’apple’a=’my name is{},I like{}’.format(name,fruit)b=’my name is{1},I like{0}’.format(fruit,name)c=’my name is{mingzi},I like{shuiguo}’.format(shuiguo=fruit,mingzi=name)print(a) # my name is ske,I like appleprint(b) # my name is StivenWang,I like appleprint(c) # my name is StivenWang,I like apple——————————————————————————————-”’index(…) 检测字符串string中是否包含子字符串 str , 如果存在,则返回str在string中的索引值, 如果指定beg(开始)和 end(结束)范围,则检查是否包含在指定范围内, 该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常(ValueError: substring not found)。 S.index(sub[, start[, end]]) -> int Like S.find() but raise ValueError when the substring is not found.None”’# print(help(str.index))a=”this is string example….wow!!!”b=”exam”#c=a.index(b)d=a.index(b, 20)#print(c) # 15 返回b字符串的第一个字符的索引值print(d) # 报错——————————————————————————————-”’isalnum(…) 检测字符串是否由字母或数字组成 S.isalnum() -> bool Return True if all characters in S are alphanumeric and there is at least one character in S, False otherwise.None”’print(help(str.isalnum))a=”thisisstringexamplewow”b=”123″c=’asdf1234’d=’asdf!@@#’e=’a=”this is string example wow”‘print(a.isalnum()) # True 全部由字母构成print(b.isalnum()) # True 全部由数字构成print(c.isalnum()) # True 全部由字母或者数字构成print(d.isalnum()) # False 有特殊字符print(e.isalnum()) # False 有空格——————————————————————————————-”’isalpha(…) 检测字符串是否只由字母组成 S.isalpha() -> bool Return True if all characters in S are alphabetic and there is at least one character in S, False otherwise.None”’print(help(str.isalpha))a=”thisisstringexamplewow”b=”123″c=’asdf1234’d=’asdf!@@#’e=’a=”this is string example wow”‘print(a.isalpha()) # True 全部由字母构成print(b.isalpha()) # False 有数字print(c.isalpha()) # False 有数字print(d.isalpha()) # False 没有字母print(e.isalpha()) # False 有空格——————————————————————————————-
篇幅有限无法放入所有demo,兴趣的读者可以私聊我或者加群:928233686领取我的所有笔记。
更多资料尽在群:928233686,欢迎加群交流。
主题测试文章,只做测试使用。发布者:艾迪号,转转请注明出处:https://www.cqaedi.cn/baike/46959.html