今日内容概要
- 习题讲解
- 字符串的内置方法
- 列表的内置方法
- 可变类型与不可变类型
今日内容详细
习题讲解
"""
温馨提示:
 ⽤户名与密码源于字符串 source_data='kevin|123'
 想办法从中拆分出⽤户名和密码⽤于后续账户信息⽐对
 普通要求:
 	1. 验证失败情况下可⼀直循环验证,成功则直接退出
 拔⾼练习:
     1. 只允许三次失败机会
     2. 登录成功后进⼊内层循环,⽤户输⼊任何指令利⽤格式化
输出
 	打印正在执⾏该⽤户指令即可,直到⽤户输⼊字⺟q退出内层
循环
"""
source_data='kevin|123'
# 1.解压赋值
username,password = source_data.split('|')
# 5.添加计数器
count = 1
# 4.循环
while True:
    if count == 4:
        break
    # 2.用户输入
    in_name = input("请输入用户名>>>:").strip()
    in_password = input("请输入密码>>>:").strip()
    # 3.用户名密码判断
    if username == in_name and password == in_password:
        print("登陆成功")
        # 6.用户输入指令
        cmd = input("想要执行的指令>>>:").strip()
        if cmd == 'q':
            break
        print("正在执行的指令>>>%s" % cmd)
    else:
        print("登陆失败")
        count += 1
字符串的内置方法
1.大小写转换   str.upper()       str.upper()
1.将字符串转换为大写 str.upper(self)
	str1 = 'KEvin give ME 12$'
    print(str1.upper())  # KEVIN GIVE ME 12$
2.将字符串转换为小写 str.lower(self)
	str1 = 'KEvin give ME 12$'
    print(str1.lower())  # kevin give me 12$
ps:运用于图片的验证码不区分大小写
3.练习
ag:把用户输入的验证码全部转为大写或者小写,跟原来的验证码都转为大写或者小写进行比较
    """
把用户输入的验证码全部转为大写或者小写,跟原来的验证码都转为大写或者小写进行比较
"""
jpg1 = 'Ds45TJ'
while True:
    in_jpg = input("请输入图片上的验证码>>>:").strip()
    if jpg1.upper() == in_jpg.upper():
        print("正确")
        break
    else:
        print("验证码输入错误")
2.判断字符串是否为大小写    str.isupper()    str.islower()
1.判断字符串中的字母是否为大写 str.isupper(self)
    str1 = 'KEvin give ME 12$'
    str2 = 'KEVin'
    str3  = 'KEVIN123'
    str4 = 'kevin'
    print(str1.isupper())  # False
    print(str2.isupper())  # False
    print(str3.isupper())  # True
    print(str4.isupper())  # False
2.判断字符串是否为小写 str.islower(self)
    str1 = 'KEvin give ME 12$'
    str2 = 'KEVin'
    str3  = 'kevin123'
    str4 = 'kevin'
    print(str1.islower())  # False
    print(str2.islower())  # False
    print(str3.islower())  # True
    print(str4.islower())  # True
3.判断字符是否以某个字符开头或结尾   str.startswith()  str.endswith()
1.判断字符是否以某个字符开头 str.startswith(self, prefix, start=None, end=None)
    res = 'Kevin123 OldGirl'
    print(res.startswith('kev'))  # False
    print(res.startswith('Kev'))  # True
2.判断字符是否以某个字符结尾 str.endswith(self, suffix, start=None, end=None)
    res = 'Kevin123 OldGirl'
    print(res.endswith("GIRL"))  # False
    print(res.endswith('rl'))  # True
1.format方法格式化
    1.1 按照顺序
        str1 = 'my name is {},my age is {}'
        print(str1.format('nana',18))  # my name is nana,my age is 18
    1.2  按照索引
        str1 = '{1} {1}my name is {0},my age is {1} {0}{0}'
        print(str1.format('tony',18))  # 18 18my name is tony,my age is 18 tonytony
    1.3 按照变量名
        str1 = 'my name is {name},my age is {age}'
        print(str1.format(name= 'kevin',age = 18))  # my name is kevin,my age is 18
5.字符串的拼接     +    str.join()
1.+号
    print('hello' + ' '+'world')  # hello world
    list1 = ['nana','tony','tom']
    print(list1[0] +"|"+ list1[1] + "|"+ list1[2])  # nana|tony|tom
2.str.join(self, iterable)
    list1 = ['nana','tony','tom']
    print('|'.join(list1))  # nana|tony|tom
6.替换字符串    str.replace()
1.str.replace(self, old, new, count=None)
    s = 'my name is kevin kevin kevin kevin'
    print(s.replace('kevin','jason',2))  # my name is jason jason kevin kevin
    print(s.replace('kevin','jason'))  # my name is jason jason jason jason
7.判断是否为纯数字
1.str.isdigit(self)
    s1 = '123.1'
    s2 = '123'
    print(s1.isdigit())  #  False
    print(s2.isdigit())  # True
8.查找一个子字符串在另一个字符串中 的索引 str.find()  str.index()
1.str.find(self, sub, start=None, end=None)
    msg='tony say hello hello hello'
    print(msg.find('say'))  # 5
    print(msg.find('hello'))  #  9
    print(msg.find('hellllo'))  # -1
2.str.index(self, sub, start=None, end=None)   
    print(msg.index('say'))  # 5
    print(msg.index('hello'))  #  9
    print(msg.index('hellllo'))  # ValueError: substring not found
3.index 与find区别
	如果一个子字符串在一个字符串里面,那么久返回子字符串第一个字母在字符串里的索引;如果不在,find()方法会返回数字-1,index()方法会报错
9.记录子字符串出现的次数   str.count()
1.str.count(self, sub, start=None, end=None)
s1 = 'hello jello helllo  world'
print(s1.count('hello')) # 1
print(s1.count('l')) # 8
print(s1.count('m'))  # 0
10.居中展示 左对齐 右对齐  str.center()   str.ljust()   str.rjust()
1.居中展示 str.center(self, width, fillchar=None)
    s1 = 'tony'
    print(s1.center(12,'+'))  # ++++tony++++
2.左对齐 str.ljust(self, width, fillchar=None)
    s1 = 'tony'
    print(s1.ljust(12,'+'))  # tony++++++++
3.右对齐 str.rjust(self, width, fillchar=None)
    s1 = 'tony'
    print(s1.rjust(12,'+'))  # ++++++++tony
11.字符串大小写格式   str.title()   str.capitalize()    str.swapcase()
1.字符串中,单词首字母大写 str.title(self)
    str1 = 'my name is  jason'
    print(str1.title())  # My Name Is  Jason
2.字符串中,第一个单词大写,其余小写   str.capitalize(self)  
    str1 = 'my name is  jason'
    print(str1.capitalize()) # My name is  jason
3.字符串中,所有单词大写 str.swapcase(self)
    str1 = 'my name is  jason'
    print(str1.swapcase())  # MY NAME IS  JASON
列表的内置方法
1.基本用法
1.类型转换:能够被for循环的数据类型都可以转换成列表(字符串、集合、字典、元组)  list() 
    a = 1
    b = 11.2
    c = 'kevin'
    d = {11,22,44,33}
    e = {'name':'NANA','age':18}
    f = [11,22,33]
    g = (11,22,33,)
    print(list(a))  # TypeError: 'int' object is not iterable
    print(list(b)) # TypeError: 'float' object is not iterable
    print(list(c))  # ['k', 'e', 'v', 'i', 'n']
    print(list(d))  # [33, 11, 44, 22]
    print(list(e))  # ['name', 'age']
    print(list(f))  # [11, 22, 33]
    print(list(g))  #  [11, 22, 33]
    ps :1.整型和浮点型不能够使用该方法
        2.字符串用list()方法,返回的额是一个个的字符组成的列表
        3.字典用list()方法,返回的是key值组成的列表
        4.集合是无序的
2.列表长度 len(*args, **kwargs)
    l1 = [1,2,3,4]
    print(len(l1))  # 4
    
3.索引取值
l1 = ['jason', 'kevin', 'oscar', 'tony']
print(l1[0])  # jason
print(l1[-1])  # tony
4.切片操作
	l1 = ['jason', 'kevin', 'oscar', 'tony']
    print(l1[0:3])  # ['jason', 'kevin', 'oscar']
    print(l1[:])  # ['jason', 'kevin', 'oscar', 'tony']
    print(l1[-4:-1])  # ['jason', 'kevin', 'oscar']
4.间隔/方向
	l1 = ['jason', 'kevin', 'oscar', 'tony']
	print(l1[::2])  # ['jason', 'oscar']
2.增
1.末尾追加  list.append(self, p_object)
    list1 = [11,33,'nana','kevin']
    print(list1.append(55))  # None
    print(list1)  # [11, 33, 'nana', 'kevin', 55]
2.索引插入
	list1 = [11,33,'nana','kevin']
    print(list1.insert(2,'jason'))  # None
    print(list1)   # [11, 33, 'jason', 'nana', 'kevin']
3.列表的合并  
    list1 = [11,33,'nana','kevin']
    list1.append([1,2,3,4])
    print(list1)  # [11, 33, 'nana', 'kevin', [1, 2, 3, 4]]
    list2 = [11,33,'nana','kevin']
    list2.extend([1,2,3,4])
    print(list2) # [11, 33, 'nana', 'kevin', 1, 2, 3, 4]
	ps:append()会把列表的整体添加到末尾,extend()是列表的合并
3.删
1.del
    list1 = [1,2,3,'jason','kevin']
    del list1[1]
    print(list1)  # [1, 3, 'jason', 'kevin']
    del list1[2]
    print(list1) # [1, 3, 'kevin']
    del list1
    print(list1)  # NameError: name 'list1' is not defined
2.list.remove(self, value)  # 括号里写要删除的值
    list1 = [1,2,3,'jason','kevin']
    list1.remove('jason')
    print(list1)
    list1.remove(22) # ValueError: list.remove(x): x not in list
3.list.pop()     list.pop(self, index=None) 弹出元素 可以指定索引删除值
    list1 = [1,2,3,'jason','kevin']
    print(list1.pop())  # kevin
    print(list1)  # [1, 2, 3, 'jason']
    
    list2 = [1,2,3,'jason','kevin']
    print(list2.pop(3))  # jason
    print(list2)  # [1, 2, 3, 'kevin']
4.改与查
1.改 索引值修改
2.查 切片
可变类型与不可变类型
1.可变类型:值改变,id不变 # 列表   
    list1 = [1,2,3,4]
    print(id(list1))  # 2172934442824
    list1.append(5)
    print(id(list1))  # 2172934442824
2.不可变类型:值改变,id也改变 # 字符串 整型 浮点型 
    str1 = 'hello world'
    print(id(str1))  #1497913184048
    str1.title()
    print(id(str1.title()))  # 1497913184560
作业
'''使用列表的内置方法实现队、栈两种数据结构'''
list1 =[11,22,33,44,55]
list2 = []
# 1.先进
for i in list1:
    list2.append(i)
print(list2) # [11, 22, 33, 44, 55]
# 2.队列 先进先出
for i in list2:
    print(i)
print("***************************")
# 3.栈 先进后出
while True:
    if len(list2):
        print(list2.pop())
    else:
        break