python.study.1

image
图片中大意是chandler在觉得不自在时就会讲笑话。这是Chandler在和身旁的女友Monica前老男友Dr. Richard见面时很尴尬而说的话,的确搁谁都很尴尬~~


0x00 python 字符串运算符


image


0x01 字符串格式化


image


0x02 python三引号


python中三引号允许一个字符串跨多行存在,其中还能用换行符,制表符等其他特殊字符。

html代码实例

1
2
3
4
5
6
7
errHTML = '''
<HTML><HEAD><TITLE>
Friends CGI Demo</TITLE></HEAD>
<BODY><H3>ERROR</H3>
<B>%s</B><P>
<FORM><INPUT TYPE=button VALUE=Back ONCLICK="window.history.back()"></FORM> </BODY></HTML>
'''

mysql代码实例

1
2
3
4
5
6
cursor.execute('''
CREATE TABLE users (
login VARCHAR(8),
uid INTEGER,
prid INTEGER)
''')

0x03 Unicode 字符串


python中在字符串引号前加一个u就是Unicode编码了,so easy

1
2
3
4
test_1 = "test"
test_2 = u"test"
print type(test_1)
print type(test_2)

image


0x04 字符串操作


  • 分割 list = string.split(‘,’)
  • 合并 ‘,’.join(list)
  • 搜索 ‘1111,22222’.find(‘,’)
  • 替换 “aaaacdef”.replace(“aaaa”, “ab”);
  • 长度 len(“aaaacdef”)
  • 去处首尾空格 “abcdef \n”.strip()

练习代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-
import requests,re,
r = requests.get(u"http://www.nyist.edu.cn")
r.encoding = r.apparent_encoding #获取编码格式
html = r.text
#!!!find!!!#
#***获取标题***#
# start_title = html.find("<title>")
# end_title = html.find("</title>")
# title = html[start_title+7:end_title]
# #***获取地理位置***#
# start_location = html.find("Technology")
# end_location = html.find("Http://www.nyist.net")
# location = html[start_location+11:end_location-60]
# location = re.sub(" ","",location)
# #!!!find!!!#
#!!!split!!!#
title = html.split("<title>")[1].split("</title>")[0]
location = html.split("Technology ")[1].split("<a href=\"http://www.nyist.net\" style=\"margin-left:3px;\">")[0]
location = re.sub(" ","",location)
#!!!split!!!#
print u"************匹配学校的信息*******************"
print u"学校名称: ",title,"\t",
print u"长度为: ",len(title)
print u"学校位置: ",location.strip(),u"长度为:",len(location)

sublime回显:
image
cmd回显:
image


0x05 base64编码


练习代码

1
2
3
4
5
6
7
8
9
10
# -*- coding: utf-8 -*-
import base64
#~~~base64~~~#
str_bs64 = 'abcdefg'
enbs64_str = base64.b64encode(str_bs64)
debs64_str = base64.b64decode(enbs64_str)
#~~~base64~~~#
print u"************base64测试*******************"
print u"base64: ",enbs64_str
print u"base64复原: ",debs64_str

sublime回显:
image
cmd回显:
image


0x06 md5hash值


练习代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# -*- coding: utf-8 -*-
import md5,hashlib
r = requests.get(u"http://www.nyist.edu.cn")
r.encoding = r.apparent_encoding #获取编码格式
html = r.text
#@@@--MD5^hash--@@@#
#$$$-MD5-$$$#
str_html = html #网页内容
md5_str = md5.new()
md5_str.update(str_html.encode("utf-8"))
#$$$-hashlib-$$$#
hashlib_str = hashlib.md5()
hashlib_str.update(str_html.encode("utf-8"))
#@@@--MD5^hash--@@@#
print u"************md5hash测试*******************"
print u"网页md5库的hash为:",md5_str.hexdigest()
print u"网页hashlib库的hash为:",hashlib_str.hexdigest()

sublime回显:
image
cmd回显:
image

题外话:如果你对本站文章字体有任何不适,请告知我,我会回复你,字体问题请配置浏览器,调到对眼睛舒适的大小;默认不允许转载,除非转载注明出处。

end