<jupyter_start><jupyter_text>更多字符串和特殊方法- 前面我们已经学了类，在Python中还有一些特殊的方法起着非常重要的作用，这里会介绍一些特殊的方法和运算符的重载，以及使用特殊方法设计类<jupyter_code>a= 'hxj'
print(a)
type(a)
a= ' '
print(a)
type(a)<jupyter_output><jupyter_text>str 类- 一个str对象是不可变的，也就是说，一旦创建了这个字符串，那么它的内容在认为不改变的情况下是不会变的- s1 = str()- s2 = str('welcome to Python')<jupyter_code>a = 100
b = str(a)
b + 'hxj'<jupyter_output><empty_output><jupyter_text>创建两个对象，分别观察两者id- id为Python内存地址<jupyter_code>id(a)
a = 100
b = 100
print(id(a),id(b))
a = 3000
b = 100
print(id(a),id(b))
a = '3000'
b = '3000'
print(id(a),id(b))<jupyter_output>2143873977456 2143873977456<jupyter_text>处理字符串的函数- len- max- min- 字符串一切是按照ASCII码值进行比较<jupyter_code>length = len('abc')
length
length = len('abcd')
length
a = len('zgun')
b = len('gun')
a == b
c = 'Joker'
max(c)
c = 'Joker'
d = 'HAHa'
max(c,d)<jupyter_output><empty_output><jupyter_text>下角标运算符 []- 一个字符串是一个字符序列，可以通过索引进行访问- 观察字符串是否是一个可迭代序列  \__iter__<jupyter_code>c[0] #索引
print(c)
c[4]      #正索引
print(c)    #负索引
c[-1]
h = 'hei ha hei'
h[-5]
h = 'hei ha hei'
h[5]
for i in 'Joker':
    print(i)
f = 'Joker'
i = 0
while i < len(f):
    print(f[i])  
    i += 1<jupyter_output>J
o
k
e
r<jupyter_text>切片 [start: end]- start 默认值为0- end 默认值为-1<jupyter_code>1.和for loop 一样，如果步长为正，那么start < end
2.如果步长为负，那么 start > end
3.如果想要整体反转（左右），[::-1]
a = '下课要去吃饭，我们可开心了'
a[::]
a = '下课要去吃饭，我们可开心了'
a[::-1]
a = '下课要去吃饭，我们可开心了'
a [-3:-1]
def Search(index):
    path ='C:/Users/asus/Desktop/python/mail.txt'
    with open(file=path,mode='r') as f:
        for i in range(1000):
            line = f.readline()
            new_line = line[0:index+1]
            if new_line[-1] == '@':
                print(new_line[:-1])    
Search(9)<jupyter_output>326719610
519203740
258109664
690326585
275141514
442852203
350191363
295088868
279690888
373017622
349094820
328184981
457271598
462743251
349127247
165279069
385368599
327449705
245911391
553434719
154790981
277054780
742807885
651493465
472935516
270153221
281131064
352750777
139615818
404674604
669505006
654667355
327814459
929112005
895190322
494084700
616085845
545916109
819663594
327332417
345667776
287933625
645291357
397751852
811437256
350842318
369463588
262634151
412478374
294550353
441905149
646611966
420293073
273039753
906769956
362922140
370640248
442555707
284782523
434777683
195777728
gaven2005
136886754
964780391
805021028
673992339
542757803
285300892
279082878
421550662
874603938
379699205
345888324
359795798
534080117
609481068
280469268
372748477
529234962
381274203
273192670
381411531
173835671
709538225
164744460
386936906
270152141
671145230
260666622
249182135
978567526
530452274
503918221
451772348
136713731
755707606
424466022
362729671
732276554
315429494
[...]<jupyter_text>链接运算符 + 和复制运算符 *- \+ 链接多个字符串，同时''.join()也是- \* 复制多个字符串<jupyter_code>a = 'a'
b = 'b'
a+b
a = 'a'
b = 'b'
a * 10
'*'.join(['a','b'])<jupyter_output><empty_output><jupyter_text>in 和 not in 运算符- in :判断某个字符是否在字符串内 - not in ：判断某个字符是否不在字符串内- 返回的是布尔值<jupyter_code>'J' in 'Joker'
'K' in 'Joker'
'J' not in 'Joker'<jupyter_output><empty_output><jupyter_text>比较字符串-  ==, !=, >=, , <- 依照ASCII码值进行比较<jupyter_code>'a' > 'b'
'a' >= 'b'<jupyter_output><empty_output><jupyter_text>测试字符串![](../Photo/99.png)- 注意：> - isalnum() 中是不能包含空格，否则会返回False<jupyter_code>password = input('请输入密码:>>')
N1 = 0 #大写
N2 = 0 #小写
N3 = 0 #数字
if len(password) <= 8:
    print('密码必须大于8位')
for i in password:
    if i.isupper():
        N1 +=1
    if i.islower():
        N2 +=1
    if i.isdigit():
        N3 +=1
if N1 ==0:
    print('密码必须有大写')
if N2 ==0:
    print('密码必须有小写')
if N3 ==0:
    print('密码必须含有数字')<jupyter_output>请输入密码:>>GJ<PASSWORD>2
密码必须大于8位
密码必须有小写<jupyter_text>搜索子串![](../Photo/100.png)<jupyter_code>b = 'abhxkkk'
b.count('k')
def Search(index):
    path ='C:/Users/asus/Desktop/python/mail.txt'
    with open(file=path,mode='r') as f:
        for i in range(1000):
            line = f.readline()
            if line.find('32671961') != -1:
                print(line)
    
Search(8)<jupyter_output><EMAIL><jupyter_text>转换字符串![](../Photo/101.png)  删除字符串![](../Photo/146.png)<jupyter_code>a = ' he xiao jing '
print(a)
a.strip()
b = 'he xiao meng'
print(b)
b.rstrip('meng')
******
*    *
*    *
*    *
******<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 = 'abcd'
for i in a:
    print(i)
f = 'abcd'
i = 0
while i < len(f):
    print(f[i])  
    i += 1<jupyter_output>a
b
c
d<jupyter_text>Homework- 1![](../Photo/106.png)<jupyter_code>def anquan():#定义函数
    haoma='123-45-6789'#赋值
    a=eval(input('请输入社会安全号:>>'))
    if haoma==a: #条件
         print('Valid SSN')
    else:
         print('Invalid SSN')  
anquan()<jupyter_output>请输入社会安全号12-56-78
Invalid SSN<jupyter_text>- 2![](../Photo/107.png)<jupyter_code>def abc(): #定义
    a=input('请输入一个字符串')
    b=input('请输入一个字符串')
    if a.find(b)==-1:  #条件
        print('不是子串')
    else:
        print('是子串')
abc()
abc()<jupyter_output>请输入一个字符串'a','v','2','g','8'
请输入一个字符串'2'
是子串<jupyter_text>- 3![](../Photo/108.png)<jupyter_code>password = input('请输入密码:>>')
N1 = 0 #字母
N2 = 0 #数字
if len(password) <= 8:
    print('密码必须大于8位')
for i in password:
    if i.isupper():
        N1 +=1
    if i.isdigit():
        N2 +=1
    if N1 !=0 and N2>2:
            print('valid password')
    elif N1==0 and N2<2:
            print('invalid password')<jupyter_output>请输入密码:>><PASSWORD>
invalid password<jupyter_text>- 4![](../Photo/109.png)<jupyter_code>def countLetters(*arge):#定义函数
    a = eval(input('请输入:>>'))
    daxie = 0
    xiaoxie = 0
    for i in a:
        ASCII = ord(i)
        if 65<=ASCII<=90: #条件
            daxie += 1
        elif 97<=ASCII<=122:
            xiaoxie += 1
    return daxie+xiaoxie
countLetters()<jupyter_output>请输入:>>'1','q','A','2','P'<jupyter_text>- 5![](../Photo/110.png) - 6![](../Photo/111.png)<jupyter_code>def reverse():#定义函数
    s= input('请输入：>>')
    return s[::-1] #切片
reverse()<jupyter_output>请输入：>>123456<jupyter_text>- 7![](../Photo/112.png) - 8![](../Photo/113.png) - 9![](../Photo/114.png)<jupyter_code>class Rectangle:   
    def __init__(self):
        print('初始化')  #初始化
    def A(self):
        self.width=eval(input('请输入长:>>'))#输入长
        self.height=eval(input('请输入宽:>>'))#输入宽
    def getArea(self):
        self.S=self.width*self.height#长方形面积公式长*宽
        print('面积是:>>',self.S)
    def getPerimeter(self):
        self.F=2*(self.width+self.height)#长方形周长（A+B）*2
        print('周长是:>>',self.F)
hxj=Rectangle()
hxj. A()
hxj.getArea()
hxj. getPerimeter()<jupyter_output>周长是:>> 91.0
{'fraction_non_alphanumeric': 0.17513870145449092, 'fraction_numerical': 0.17049032838506523, 'mean_word_length': 4.041572184429327, 'pattern_counts': {'":': 0, '<': 78, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '445053', 'license': 'Apache-2.0', 'n_tokens_mistral': 4130, 'n_tokens_neox': 3629, 'n_words': 524, 'path': '7.24.ipynb', 'repo_name': 'duzihexiaojing/Python', 'size': '6670'}