Python学习(str字符串,附带源码解释,中文翻译和各种实例)

Python学习(str字符串,附带源码解释,中文翻译和各种实例)因为一旦网上爆出某个漏洞,你得会用Python去编写EXP利用它呀。这样可以避免没必要的人力,直接脚本检测就ok啦。

欢迎大家来到IT世界,在知识的湖畔探索吧!

渗透之Python学习(str字符串)

感谢大家的关注,小编我今天又要挖一个坑啦,分享Python的学习笔记。

都说网络安全这条路,python语言是必学的。为什么呢?因为一旦网上爆出某个漏洞,你得会用Python去编写EXP利用它呀。这样可以避免没必要的人力,直接脚本检测就ok啦。如果你对这个没有详细的概念,可以看我前面的那篇redis获取服务器权限的文章。只要运行脚本,就可以直接代替人省略了十多步操作,直接获取权限,是不是很方便呢。

从现在起我会不间断的分享我的Python学习笔记,供大家学习。

因为篇幅字数有限,没有办法把所有的笔记都放在这里。有兴趣的读者可以私聊我或者加群:928233686领取我的所有笔记。

下图是我要不断分享的Python模块笔记,附带源码解释,中文翻译和各种实例,让你对该模块有更深入的理解。

Python学习(str字符串,附带源码解释,中文翻译和各种实例)

# 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,
# 如果需要的话,就用fillchar进行填充,默认是空格。但是不会对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"为检索字符串的开始与结束位置。
'''
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 = True
print(c) # c = False
print(d) # d = True
print(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 = True
print(c) # c = False
print(d) # d = True
print(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 apple
print(b) # my name is StivenWang,I like apple
print(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 有空格
-------------------------------------------------------------------------------------------

欢迎大家来到IT世界,在知识的湖畔探索吧!

篇幅有限无法放入所有demo,兴趣的读者可以私聊我或者加群:928233686领取我的所有笔记。

更多资料尽在群:928233686,欢迎加群交流。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/37271.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们YX

mu99908888

在线咨询: 微信交谈

邮件:itzsgw@126.com

工作时间:时刻准备着!

关注微信