<jupyter_start><jupyter_text>更多字符串和特殊方法- 前面我们已经学了类，在Python中还有一些特殊的方法起着非常重要的作用，这里会介绍一些特殊的方法和运算符的重载，以及使用特殊方法设计类  str 类- 一个str对象是不可变的，也就是说，一旦创建了这个字符串，那么它的内容在认为不改变的情况下是不会变的- s1 = str()- s2 = str('welcome to Python')<jupyter_code>a = 'zzz'
print(a)
b = str([1,2,3])
print(b)<jupyter_output>[1, 2, 3]<jupyter_text>创建两个对象，分别观察两者id- id为Python内存地址<jupyter_code>print(id('a'))
print(id('a'))
print(id('a'))
print(id('b'))
a = 'a'
b = 'a'
print(id(a))
print(id(b))
#数字在2000以内共用同一个IP地址
a = 10000
b = 10000
print(id(a))
print(id(b))
a = 'a'
b = 'aaaa'
print(id(a))
print(id(b))<jupyter_output>34733728
87620552<jupyter_text>处理字符串的函数- len- max- min- 字符串一切是按照ASCII码值进行比较<jupyter_code>len('abc')
len(' abc ')
a = 'abc'
a[0]
i = 0
a = 'abc'
while i < len(a):
    print(a[i])
    i += 1
len('虎王')<jupyter_output><empty_output><jupyter_text>下角标运算符 []- 一个字符串是一个字符序列，可以通过索引进行访问- 观察字符串是否是一个可迭代序列  \__iter__<jupyter_code>a = 'jingwang'
a[0:1]
a = 'jingwang'
a[3:5]
a = 'jingwang'
a[::-1]<jupyter_output><empty_output><jupyter_text>切片 [start: end]- start 默认值为0- end 默认值为-1<jupyter_code>a = 'abcdef'
a[1::2]
a = 'https://www.baidu.com'
a[12:17]
a = 'jingwang'
a[3:5]
a = 'jingwang'
a[0]<jupyter_output><empty_output><jupyter_text>链接运算符 + 和复制运算符 *- \+ 链接多个字符串，同时''.join()也是- \* 复制多个字符串<jupyter_code>'s'+'b'
for i in range(5,0)
if i % 2 == 0
print('*' * i)


 for i in range(5): 
    if i % 2 == 0:
        print("*", end="")
    else:
        print(" ", end="") 
        print()
        for j in range(2): 
            for i in range(2):
                    if i == 0: 
                        print("*", end="") 
                    elif i == 18: 
                        print("*")
                        break
                    else: 
                        print(" ", end="")
                        for i in range(2): 
                            if i % 2 == 0:
                                print("*", end="") 
                            else: 
                                print(" ", end="")
for i in range(0,4):
    if i == 0 or i == 3:
        print('*' * 5)
    else:
        print('*'+' '* 3 + '*')
url='http://op.hanhande.net/shtm1/op_wz/list_2602_'<jupyter_output><empty_output><jupyter_text>in 和 not in 运算符- in :判断某个字符是否在字符串内 - not in ：判断某个字符是否不在字符串内- 返回的是布尔值<jupyter_code>'a' in 'aaa'

a = '66546'
b = '5'
b in a
a = '66546'
b = 'a'
b not in a
def lll(a,b):
    for i in a:
        for j in b:
            if i == j:
                return True
            else:
                return False
a = 'asdfg'
b = 'fb'
b in a
a = 'asdfg'
b = 'f'
b in a
lll(a,b)
path = '/Users/Administrator/Desktop/python/mail.txt'
with open(path) as f:
    for i in range(5000):
        lines = f.readline()
        a = lines[0]
        if a == '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>

12<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>'a' > 'aa'
'c*2' > 'a*2'
a = 'abcse'
b = a[3:5]
c = a[0]
d = a[2]+a[0]
print(b,c,d)<jupyter_output>se a ca<jupyter_text>测试字符串![](../Photo/99.png)- 注意：> - isalnum() 中是不能包含空格，否则会返回False<jupyter_code>def hh():
    m = input('>> ')
    if len(m) = 8 and m.issupper()=True and m.islower()=True and m.isdigit()=True:
        print()
    elif len(m) < 8:
        print()
    elif  m.issupper()=False:   
    
def hh(m):
    """
    Check Password
    Parameters:
    ----------
        password:Need Check Password.
    Returns:
    -------
        None.
    Note:
    ----
        1.Must use for loop
        2.必须写上注释
    """
    N = len(a)
    N1,N2,N3 = 0,0,0
    for i in a:
        if i.issupper():
            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位数')
    
hh()
b = 'asd'
b.islower()
a = '!'
a.isalnum()
b = 'sum'
a.isidentifier()
#只能是字母或数字
a = '123scd'
a.isalnum()
a = '123scd'
a.isalpha()<jupyter_output><empty_output><jupyter_text>搜索子串![](../Photo/100.png)<jupyter_code>d = 'asddga'  
d.rfind('a')#rfind == right find(从有开始找，从左数)
d.find('a')#从左开始找
d.find('n')
c = 'asd'
c.rfind('a')
b.count('帅!')
a = '帅'
b = '帅帅'
b.startswith(a)
import requests
url = 'http://www.4399dmw.com/haizeiwang/tupian/'
HTML= requests.get(url).text
LIST = HTML.split('\n')
for i in LIST:
    if "海贼王" in i:
        print(i)
url = 'http://www.4399dmw.com/haizeiwang/tupian/'
HTML= requests.get(url).text
        
if '海贼王' in HTML: 
    print(HTML)
import requests
url = 'http://www.4399dmw.com/haizeiwang/tupian/'
HTML= requests.get(url).text
LIST = HTML.split('\n') 
for line in LIST:
    if '.jpg' in line:
        lin2 = line.split('"')
#       print(lin2)
        for linee in lin2:
            if '.jpg' in linee:
                print(linee)
url = 'http://www.4399dmw.com/haizeiwang/tupian/'
HTML= requests.get(url).text
LIST = HTML.split('\n')
for line in LIST:
    if '.jpg' in line:
        lin2 = line.split('"')
#       print(lin2)
        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.swapcase()
a = 'joker'
a.capitalize()<jupyter_output><empty_output><jupyter_text>删除字符串![](../Photo/146.png)<jupyter_code>a = '    sfd   '
a.strip()<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)  Homework- 1![](../Photo/106.png)<jupyter_code>def num_():
    num = input('输入数字:')
    if num == num_:
        print('Valid SSN')
    else:
        print('Invalid SSN')
num_()
import random
a = eval(input("按照格式ddd-dd-dddd输入社会安全号码："))
d = random.random(0,9)
if a in ddd-dd-dddd:
    print("Valid SSN")
else:
    print("Invalid SSN")<jupyter_output>按照格式ddd-dd-dddd输入社会安全号码：45nm451<jupyter_text>- 2![](../Photo/107.png)<jupyter_code>def find():
    a = input("请输入字符串a：")
    b = input("请输入字符串b:")
    if a in b:
        print("a是b的子串")
    else:
        print("a不是b子串")
find()<jupyter_output>请输入字符串a：45985
请输入字符串b:52
a不是b子串<jupyter_text>- 3![](../Photo/108.png)<jupyter_code>def cheak(a):
    n = len(a)
    n1,n2 = 0,0
    for i in a:
        if i.isalnum():
            n1 += 1
        elif i.isdigit():
            n2 += 1
    if n <= 8:
        print('密码必须至少有8个字符')
    if n1 == 0:
        print('密码只能包含英文字母和数字')
    if len(str(n2)) < 2:
        print('密码应该至少包含两个数字')
cheak('fg52fg')
a = input("请输入密码：")
    N = len(a)
    N1,N2,N3 = 0,0,0
    for i in a:
        if i.issupper():
            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位数')<jupyter_output><empty_output><jupyter_text>- 4![](../Photo/109.png)<jupyter_code>def countletters():
    a = input("请输入字符串：")
    i = 0
    for b in a:
        b = a.isalpha()  
        i += 1
        c = len(b)
        print(c)
countletters()
def char_():
    b=str(input('>>'))
    N=0#计数
    for i in b:
        if i.isupper():
            N+=1
        if i.islower():
            N+=1
    return N
char_()<jupyter_output>>>ll,<jupyter_text>- 5![](../Photo/110.png)<jupyter_code>def getNumber(uppercaseletter):<jupyter_output><empty_output><jupyter_text>- 6![](../Photo/111.png)<jupyter_code>class Solution():
    def reverse(self, s):
        if len(s) > 1:
            reversed_s = ''.join(reversed(s))
            return reversed_s 
        return s
haha=Solution()
haha.reverse('fgk')<jupyter_output><empty_output><jupyter_text>- 7![](../Photo/112.png) - 8![](../Photo/113.png)<jupyter_code>class num_():
    def num(self,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12):
        self.d1 = d1
        self.d2 = d2
        self.d3 = d3
        self.d4 = d4
        self.d5 = d5
        self.d6 = d6
        self.d7 = d7
        self.d8 = d8
        self.d9 = d9
        self.d10 = d10
        self.d11 = d11
        self.d12 = d12
        d13 = 10-(d1+3*d2*d3+3*d4+d5+3*d6+d7+3*d8+d9+3*d10+d11+3*d12)%10
        if d13 == 10:
            d13.replace('10','0')
            print(self.d1,self.d2,self.d3,self.d4,self.d5,self.d6,self.d7,self.d8,self.d9,self.d10,self.d11,self.d12,d13)
hhh=num_()
hhh.num(1,2,3,4,5,6,7,8,9,1,2,3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18427666633155726, 'fraction_numerical': 0.08374384236453201, 'mean_word_length': 2.5201698513800426, 'pattern_counts': {'":': 0, '<': 132, '<?xml version=': 0, '>': 136, 'https://': 1, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '30480', 'license': 'Apache-2.0', 'n_tokens_mistral': 5266, 'n_tokens_neox': 4963, 'n_words': 736, 'path': '7.24.ipynb', 'repo_name': 'liushengkun260042/mysql', 'size': '10438'}