<jupyter_start><jupyter_text>更多字符串和特殊方法- 前面我们已经学了类，在Python中还有一些特殊的方法起着非常重要的作用，这里会介绍一些特殊的方法和运算符的重载，以及使用特殊方法设计类  str 类- 一个str对象是不可变的，也就是说，一旦创建了这个字符串，那么它的内容在认为不改变的情况下是不会变的- s1 = str()- s2 = str('welcome to Python')  创建两个对象，分别观察两者id- id为Python内存地址  处理字符串的函数- len- max- min- 字符串一切是按照ASCII码值进行比较<jupyter_code>a='abcd'
len(a)<jupyter_output><empty_output><jupyter_text>a 下角标运算符 []- 一个字符串是一个字符序列，可以通过索引进行访问- 观察字符串是否是一个可迭代序列  \__iter__  切片 [start: end]- start 默认值为0- end 默认值为-1<jupyter_code>b = [[1,[0.1,0.2],2,[4,5],3],[4,[400,500],5,[100,1000],6]]
b[-2][-4][-2]
b[1][3][0]
b[0][1][0]
b[-1][-4][-2]
a[0:-1]
a[:] ##保留后面所有
a[-3:-1]<jupyter_output><empty_output><jupyter_text>链接运算符 + 和复制运算符 *- \+ 链接多个字符串，同时''.join()也是- \* 复制多个字符串<jupyter_code>a=input('a')<jupyter_output><empty_output><jupyter_text>in 和 not in 运算符- in :判断某个字符是否在字符串内 - not in ：判断某个字符是否不在字符串内- 返回的是布尔值  比较字符串-  ==, !=, >=, , <- 依照ASCII码值进行比较  测试字符串![](../Photo/99.png)- 注意：> - isalnum() 中是不能包含空格，否则会返回False  搜索子串![](../Photo/100.png)<jupyter_code>a='艹,sb,s.b,S.B,cao'
a.count('cao')<jupyter_output><empty_output><jupyter_text>转换字符串![](../Photo/101.png)<jupyter_code>import captcha
from captcha.image import ImageCaptcha
import numpy as np
from PIL import Image
import random
import sys
number = ['1','2','3','4','5','6','7','8','9','0']
Alphabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
def random_captcha_text(char_set = number, captcah_size = 4):
    # 验证码列表
    captcha_text = []
    for i in range(captcah_size):
        # 随机选择
        c = random.choice(char_set)
        # 加入验证码列表
        captcha_text.append(c)
    return captcha_text
# 生成字符串对应列表
def gen_captcha_text_and_image():
    image = ImageCaptcha()
    # 获得随机生成验证码
    captcha_text = random_captcha_text()
    # 把验证码转成字符串
    captcha_text = ''.join(captcha_text)
    # 生成验证码
    captcha =  = image.generate(cap(captcha_text)
       image.write(cap(captcha_text,'/Users/huwang/Desktop/images/' + captcha_text + '.jpg') # 写入文件<jupyter_output><empty_output><jupyter_text>删除字符串![](../Photo/146.png)<jupyter_code>a='    a   b  cd  ef     '
a.replace(' ','')
b=a.strip(' ')
print (b)<jupyter_output><empty_output><jupyter_text>格式化字符串![](../Photo/103.png)  EP:- 1![](../Photo/104.png)- 2     随机参数100个数字，将www.baidu.com/?page=进行拼接<jupyter_code>s1="Welocme to Python"
s2=s1
s3="Welocme to Python"
s4="to"
s1.find(s4) 
min(s1)
s1[-4]
s1+s2<jupyter_output><empty_output><jupyter_text>Python高级使用方法 -- 字符串- 我们经常使用的方法实际上就是调用Python的运算重载![](../Photo/105.png)  Homework- 1![](../Photo/106.png)<jupyter_code>password=input('按照格式<PASSWORD>输入一个社会安全号码,d是数字')
num=password.split('-')
a=0
if len(password)==11:
    if num[0].isdigit() and len(num[0])==3:
        a+=1
    if num[1].isdigit() and len(num[1])==2:
        a+=1
    if num[2].isdigit() and len(num[2])==4:
        a+=1
    if a==3:
        print('Valid SSN')
    else:
        print('Invalid SSN')
else:
        print('Invalid SSN')
a=input('ddd-dd-dddd : ')
if a.isdigit==True():
    print ('Valid SSn')
else:
    print('Invalid SSN')<jupyter_output><empty_output><jupyter_text>- 2![](../Photo/107.png)<jupyter_code>test1='to be or not to be'
test1.rfind('to')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2272863568215892, 'fraction_numerical': 0.03568215892053973, 'mean_word_length': 5.143646408839779, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 40, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '525396', 'license': 'Apache-2.0', 'n_tokens_mistral': 1816, 'n_tokens_neox': 1870, 'n_words': 191, 'path': '7.24.ipynb', 'repo_name': 'gaogaoZ/gaozhe', 'size': '3336'}