<jupyter_start><jupyter_text>更多字符串和特殊方法- 前面我们已经学了类，在Python中还有一些特殊的方法起着非常重要的作用，这里会介绍一些特殊的方法和运算符的重载，以及使用特殊方法设计类  str 类- 一个str对象是不可变的，也就是说，一旦创建了这个字符串，那么它的内容在认为不改变的情况下是不会变的- s1 = str()- s2 = str('welcome to Python')<jupyter_code>a='zzz'
print(a)
b = str()
b
#str强制类型转换
b=str(2342333)
b<jupyter_output><empty_output><jupyter_text>创建两个对象，分别观察两者id- id为Python内存地址<jupyter_code>print(id('a'))
print(id('a'))
a='a'
b='a'
print(id('a'))
print(id('b'))
a=2000
b=5000
print(id('a'))
print(id('b'))<jupyter_output>2000601786664
2000601784984<jupyter_text>处理字符串的函数- len- max- min- 字符串一切是按照ASCII码值进行比较<jupyter_code>#len返回字符串长度
len('abc')
i=0
a='ancde'
while i<len(a):
    print(a[i])
    i += 1<jupyter_output>a
n
c
d
e<jupyter_text>下角标运算符 []- 一个字符串是一个字符序列，可以通过索引进行访问- 观察字符串是否是一个可迭代序列  \__iter__<jupyter_code>a = 'wanghaha'
a[7]<jupyter_output><empty_output><jupyter_text>切片 [start: end]- start 默认值为0- end 默认值为-1<jupyter_code>a='wanghaha'
a[3:5]#前闭后开的区间
a='wanghaha'
a[::]
a='wanghaha'
a[-1]#相当于len-1
#打印奇数位
b='abcdef'
b[0:6:2]
#打印偶数位
b='abcdef'
b[1:6:2]<jupyter_output><empty_output><jupyter_text>链接运算符 + 和复制运算符 *- \+ 链接多个字符串，同时''.join()也是- \* 复制多个字符串<jupyter_code>'s'+'b'
's'*2
#用*打印正方形
for i in range(5):
    for j in range(5):
        if i == 0 or i == 4:
            print('*',end=" ")
        elif j == 0 or j==4:
            print('*',end=" ")
        else:
            print(' ',end=' ')
    print(' ')
        
for i in range(0,4):
    if i ==0 or i==3:
        print("*"*5)
    else:
        print('*'+' '*3+'*')
'!'.join('abc')
import requests<jupyter_output><empty_output><jupyter_text>in 和 not in 运算符- in :判断某个字符是否在字符串内 - not in ：判断某个字符是否不在字符串内- 返回的是布尔值<jupyter_code>'a' in 'aaa'
a = '字符串'
b = '哈'
b in a
a = '字符串'
b = '哈'
b not in a
haha = '/Users/123/Desktop/python基础/mail.txt'
with open(haha) as f:
    for i in range(5000):
        lines = f.readline()
        if lines[0]=='1':
            print(lines)<jupyter_output><EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

<EMAIL>

100[...]<jupyter_text>比较字符串-  ==, !=, >=, , <- 依照ASCII码值进行比较<jupyter_code>'bc'>'ba'
#'abcde'输出‘de a ca"
def shuchu_():
    a = 'abcde'
    print(a[3:5],' ',a[0:1],' ',a[0:4:2])
shuchu_()<jupyter_output>de   a   ac<jupyter_text>测试字符串![](../Photo/99.png)- 注意：> - isalnum() 中是不能包含空格，否则会返回False<jupyter_code>a = '12k34'
a.isalnum()
b = 'abc'
b.isalpha()
b=' '
b.isspace()
#检验用户的密码，是否大于8位，是否有大写、小写、数字
def mima(a):
    N=len(a)
    N1,N2,N3=0,0,0
    for i in a:
        if i.isupper():
            N1 += 1
        elif i.islower():
            N2 += 1
        elif i.isdigit():
            N3 += 1
    if N1==0:
        print('密码必须含有大写')
    if N2==0:
        print('密码必须含有小写')
    if N3==0:
        print('密码必须含有数字')
    if N<=8:
        print('密码必须含有8位')
a='<PASSWORD>'
mima(a)<jupyter_output>密码必须含有大写<jupyter_text>搜索子串![](../Photo/100.png)<jupyter_code>a = '帅!'
b = '帅帅!'
b.endswith(a)
b.count('帅')
c = 'abcs'
c.find('bc')
#从右往左找，返回的是从左往右的
d = 'abcdea'
d.rfind('a')
import requests
url = 'http://www.4399dmw.com/haizeiwang/tupian/'
HTML = requests.get(url).text
LIST = HTML.split('\n')#split拆分
for line in LIST:
    if  '.jpg' in line:
        lin2 = line.split('"')
        for linee in lin2:
            if '.jpg' in linee:
                print(linee)<jupyter_output>http://dmimg.5054399.com/allimg/160217/6_160217142248_2.jpg
http://dmimg.5054399.com/allimg/160127/6_160127110842_1.jpg
http://dmimg.5054399.com/allimg/160126/6_160126100905_2.jpg
http://dmimg.5054399.com/allimg/151028/6_151028142939_1.jpg
http://dmimg.5054399.com/allimg/151027/6_151027140205_1.jpg
http://dmimg.5054399.com/allimg/151006/6_151006204107_1.jpg
http://dmimg.5054399.com/allimg/150927/6_150927153510_1.jpg
http://dmimg.5054399.com/allimg/150821/6_150821105253_2.jpg
http://dmimg.5054399.com/allimg/150728/6_150728100515_1.jpg
http://dmimg.5054399.com/allimg/150727/6_150727114156_1.jpg
http://dmimg.5054399.com/allimg/150726/6_150726014844_1.jpg
http://dmimg.5054399.com/allimg/150529/6_150529115733_1.jpg
http://dmimg.5054399.com/allimg/150505/6_150505112033_1.jpg
http://dmimg.5054399.com/allimg/150416/6_150416172603_1.jpg
http://dmimg.5054399.com/allimg/150415/6_150415170647_1.jpg
http://dmimg.5054399.com/allimg/150414/6_150414115634_1.jpg
http://dmimg.5054399.com/allimg/150407/6[...]<jupyter_text>转换字符串![](../Photo/101.png)<jupyter_code>a = 'joker'
a.capitalize()
a = 'joker'
a.swapcase()
a = 'joker'
a.replace('j','k')
a = 'jojer'
a.replace('j','k',2)<jupyter_output><empty_output><jupyter_text>删除字符串![](../Photo/146.png)<jupyter_code>#删掉前端的空格
b = ' abcd '
b.lstrip()<jupyter_output><empty_output><jupyter_text>格式化字符串![](../Photo/103.png)  EP:- 1![](../Photo/104.png)- 2     随机参数100个数字，将www.baidu.com/?page=进行拼接  Python高级使用方法 -- 字符串- 我们经常使用的方法实际上就是调用Python的运算重载![](../Photo/105.png)<jupyter_code>a = 'a'
b = 'b'
a + b
a.__add__('b')
a.__mul__(10)<jupyter_output><empty_output><jupyter_text>Homework- 1![](../Photo/106.png)<jupyter_code>def SSN_(b):#定义一个名为SSN_的函数
    c=str(b)#将b转换成字符串类型
    a = '222-22-2222'#设置一个默认的社会安全号码
    if c==a:#如果用户输入的社会安全号码 等于 默认的社会安全号码
        print('Valid SSN')
    else:
        print('Invalid SSN')
b='222-22-2222'
SSN_(b)#调用函数<jupyter_output>Valid SSN<jupyter_text>- 2![](../Photo/107.png)<jupyter_code>def zichuan(a,b):#定义一个名为zichuan的函数
    c=str(a)#将a转换成字符串类型
    d=str(b)#将b转换成字符串类型
    if c in d:#如果a在b里
        print('a是b的子字符串')
    else:
        print('a不是b的子字符串')
a='as'
b='asdf'
zichuan(a,b)#调用函数<jupyter_output>a是b的子字符串<jupyter_text>- 3![](../Photo/108.png)<jupyter_code>def mima(a):#定义一个名字为mima的函数
    N=len(a)#测密码的长度
    N1,N2,N3=0,0,0
    for i in a:#用for循环对密码的每一个字符进行检测
        if i.isupper():#检验大写
            N1 += 1
        elif i.islower():#检验小写
            N2 += 1
        elif i.isdigit():#检验数字
            N3 += 1
    if N1==0 and N2==0:
        print('密码必须含有字母')
    if N3<2:
        print('密码至少含有两个数字')
    if N<8:
        print('密码至少含有8位')
a='nj<PASSWORD>'
mima(a)<jupyter_output>密码至少含有两个数字<jupyter_text>- 4![](../Photo/109.png)<jupyter_code>def geshu(a):#定义一个geshu的函数
    b=str(a)#将a转换成字符串类型
    N=0
    for i in b:#用for循环对密码的每一个字符进行检测
        if i.isupper():#检验大写
            N+=1
        if i.islower():#检验小写
            N+=1
            
    print(N)
a = 'njkN456S'
geshu(a)<jupyter_output>5<jupyter_text>- 5![](../Photo/110.png)<jupyter_code>def getNumber(a):
    b = str(a)
    'a’,'b','c','d','e','f','g','h','i','j' = '@-_/','ABC','DEF','GHI','JKL','MNO','PQRS','TUV','WXYZ'
    for i in b:#用for循环对密码的每一个字符进行检测
        if i.isalpha():
            print(i)
            if i in 1:
                print(1)
a='@'
getNumber(a)<jupyter_output><empty_output><jupyter_text>- 6![](../Photo/111.png)<jupyter_code>#百度的，reverse()函数将列表的内容进行了反转，借助这个特性
#可以先将字符串化成列表
#利用reverse()函数进行反转后，再处理成字符串
def reverse1(a):
    b=list(a)
    b.reverse()
    print(b)
a='asdfgh'
reverse1(a)<jupyter_output>['h', 'g', 'f', 'd', 's', 'a']
{'fraction_non_alphanumeric': 0.19725463280713795, 'fraction_numerical': 0.09169526424159231, 'mean_word_length': 3.7496740547588003, 'pattern_counts': {'":': 0, '<': 129, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '587805', 'license': 'Apache-2.0', 'n_tokens_mistral': 4351, 'n_tokens_neox': 4221, 'n_words': 470, 'path': '7.24.ipynb', 'repo_name': 'xushuai1677084517/mysql', 'size': '7773'}