# name = input("请输入你的名字:")# age = int(input("请输入你的年龄:"))# msg = '''# ------- %s 的博客 -------# Name : %s# Age : %s# -------------------------# ''' % (name, name, age)# print(msg)# 小作业,判断敏感词汇# print(2**24)# s = input(">>>")# if not s:# print("不能为空")# else:# pass# a = "sflsjfslfjslfsjfs"# a1 = a.split('s')# print(a1)# a = "\noldboy\t"# a1 = a.strip()# print(a)# print(a1)# a = 'addoldboydb'# a1 = a.strip("addbd")# print(a)# print(a1)# s = 'oldBoy'# s1 = s.capitalize()# print(s)# print(s1)# s2 =s.swapcase()# print(s2)# s3 = s.upper()# s4 = s.lower()# print(s3,s4)# L1 = ['alex','wusir','太白',100,'女神']# # L1.append("xxx")# # L1.insert(2,'xxx')# # L1.extend('abc')# # L1.extend(['abc','dgjd'])# # L1.pop(2)# # L1.remove('wusir')# # L1.clear()# # del L1[2]# # del L1[::2]# # L1[1] = '3'# # L1[:3] = 'abcdef'# # for i in L1:# # print(i)# # print(L1)# # captialize# # swapcase# # center# # strip# # upper# # lower# rep = L1.pop(2)# print(L1)# L1 = ['alex', 'wusir',[1,'taibai']]# L1[1] = L1[1].upper()# L1[2][1] = L1[2][1].capitalize()# print(L1)# L1 = ['alex','wusir','taibai',100,'女神']# for i in range(len(L1)):# print(i)# a = 10# b = 100# a,b = b,a# print(a,b)# dic = {'name': 'TaiBai', 'age': 25, 'sex': '男',}# print(dic.items())# print(list(dic.items()))# for k,v in dic.items():# print(k,v)# l1 = [11, 22, 33, 44, 55, 66]# for i in range(len(l1)):# if i % 2 == 1:# l1 = del l1[i]# dic = {'k1':'v1','k2':'v2','k3':'v3','name': 'alex'}# L1 = dic.items()# for i in dic.items():# if 'k' in i:# s = 'python24期'# print(s[:2])# print(s[:6])# print(s[:])# print(s[:6:2])# s3 = s[-1:-4:-1]# print(s3)# s = 'oldBoy'# s1 = s.capitalize() capitalize capitalize capitalize capitalize# print(s1)# print(s)# swapcase() 大小写反转 *# s2 = s.swapcase() swapcase swapcase swapcase swapcase# print(s2)# s = 'oldBoy'# center 居中 设置宽度 *# s3 = s.center(20)# s3 = s.center(20, "*") center center center center center# print(s3)# ss = 'alex wusir2taibai*ritian'# # title() 非字母隔开的'单词'首字母大写 * title title title title title# s4 = ss.title()# print(s4)# s = 'oldBoy'# upper() 全大写 **# lower() 全小写 **# s5 = s.upper()# s6 = s.lower()# print(s5)# print(s6)# username = input('请输入账号:')# password = input('请输入密码:')# code = 'QerAg'.upper()# your_code = input('请输入验证码:').upper() # QERAG,Qerag.....# if username == 'alex' and password == 'SB':# if your_code == code:# print('登录成功')# username = input("请输入你的用户名:")# password = input("请输入你的密码:")# codeyzm = "yanzhengma".upper()# youcode = input("请输入你的验证码:").upper()# if username == 'xxx' and password == '123':# if youcode == codeyzm:# print("登陆成功!")# startswith('o') 判断以什么开头 **# endswith() 判断以什么结尾 **# s = 'oldBoy'# s6 = s.startswith('o')# s61 = s.startswith('old') startswith startswith startswith# s62 = s.startswith('oldBoy') endswith endswith endswith# s63 = s.startswith('o',1)# print(s6)# print(s61)# print(s62)# print(s63)# username = input('请输入账号:').strip()# password = input('请输入密码:').strip()# if username == 'alex' and password == 'SB':# print('登录成功')# username = input("请输入你的账号:").strip()# password = input("请输入你的密码:").strip()# if username == "zhangxu" and password == "123": strip strip strip strip strip# print("登陆成功")# s = 'alex wusir barry'ss = 'alex,wusir,barry'# ss1 = ',alex,wusir,barry'# split str ---> list ***# s8 = s.split() # 默认按照空格分割# s9 = ss.split(',') # 指定以','分割# s10 = ss.rsplit(",",1) #后面的1(count)为分割的次数## print(s8)# print(s9)# print(s10)# print(ss1.split(','))# print(ss.rsplit(',',0))s = 'oldboy' #capitalize swapcase center title upper lower strip startswith endswith split rsplit join# join *** join join join join join# s10 = '_'.join(s)# s11 = 'alex'.join(s)# print(s10)# print(s11)# 一个用途list ---> str# l1 = ['alex', 'wusir', 'barry'] # --> s = 'alex wusir barry'# s10 = ' '.join(l1)# print(s10)# s = '老男孩 老男孩 老男孩 alex linux python 大数据 alex'# replace *** #把前面的替换成后面的,可选次数# s101 = s.replace("老男孩",'张旭',2) #后面的数字为替换的次数# print(s101)# s11 = s.replace('alex','太白')# s12 = s.replace('alex','太白',1)# print(s12)# is 系列 ***# name = 'abc'# print(name.isalnum()) #字符串由字母或数字组成# print(name.isalpha()) #字符串只由字母组成# print(name.isdigit()) #字符串只由数字组成 str ---> int 可以作为一个判断条件#captialize swapcase center title upper lower strip split join replace isdigit#startswith endswith rsplit lstrip rstrip isalnum isalpha format# 格式化输出 format ***# res = '{} {} {}'.format('egon', 18, 'male')# print(res)# res='{1} {0} {1}'.format('egon', 18, 'male')# print(res)# res='{name} {age} {sex}'.format(sex='male',name='egon',age=18)# a = "{1} {0} {1}".format("d","c","a")# print(a)s = 'oldboy'# find 通过元素找索引,找不到返回-1 ***# index 通过元素找索引,找不到报错 ***# print(s.find('d'))# print(s.find('o')) # 可切片# print(s.find('A')) # 可切片# print(s.index('d'))# print(s.index('A'))# s1 = s.find("l")# s2 = s.find("m")# print(s1)# print(s2)# s3 = s.index("n")# print(s3)# 公共方法 ***# s = 'fjsdaklfjsdklafjsadf'# len 测量个数# print(len(s))# count 某个元素出现的次数# print(s.count('f'))#captialize swapcase center title upper lower strip lstrip rstrip startswith endswith#replace format join split rsplit isdigit isalnum isalpha len count find index# s1 = 'fdksj'#for 变量 in iterable: 有限循环# for i in s1:# print(i+'alex')# 只要循环被break打断,则不执行else程序。# for i in s1:# print(i)# if i == 'k':# break# else:# print(666)# l1 = ['alex', 'wusir', 'wusir', 'wusir','太白', 100, '女神']# 增 三种方式:append insert extend# append行尾追加 append append append append append# l1.append('文刚')# print(l1.append('文刚'))# print(l1)# insert 在哪里插入什么,可以自己选定位置# l1.insert(1,'oldboy')# print(l1)# extend() 迭代的追加# l1.extend('abc')# l1.extend(['abc', 'erf'])# print(l1)# append insert extend# 删# pop 按照索引删除,返回值# ret = l1.pop(1)# print(ret)# print(l1)# remove# l1.remove('alex')# print(l1)# clear# l1.clear()# print(l1)# l1 = ['alex', 'wusir', 'wusir', 'wusir','太白', 100, '女神']# del# 按照索引# del l1[1]# print(l1)# 按照切片(步长)# del l1[:2]# print(l1)# del l1[::2]# print(l1)# l1 = ['alex', 'wusir', 'wusir', 'wusir','太白', 100, '女神']# 改# 按照索引# l1[1] = 'SB'# print(l1)# 按照切片(步长)# l1[:2] = 'fsdjksdhfjsdf' #迭代加入# l1[::2] = 'abcd' # 加上步长一定要一一对应。必须一一对应,少了不行会报错# l1[::2] = ['alex', [12,3], 'c','d'] # 加上步长一定要一一对应。# print(l1)# 查# 索引,切片,切片(步长)# for# for i in l1:# print(i)# l1 = ['alex', 'wusir', 'wusir', 'wusir','太白', 100, '女神']# 其他方法:# print(l1.index('太白')) #通过元素查索引# len()# print(len(l1))#count# print(l1.count('wusir'))# l1 = [2, 3, 5, 1, 9, 8, 7, 6]# l1.sort() # 从小到大排序# print(l1)# l1.sort(reverse=True) # 从大到小排序# print(l1)# l1.reverse() # 翻转# print(l1)# print(l1)# why:# where:# l1 = ['alex', 'wusir', [1,'taibai']]# 'wusir'全部大写# l1[1] = l1[1].upper()# print(l1)# 'taibai'首字母大写# l1[-1][-1] = l1[-1][-1].capitalize()# print(l1)# print(l1[-1][-1])# range 可以看做自定义的数字范围的列表,一般都是与for循环结合。可以设置步长# a = range(1, 100)# print(a)# for i in range(10):# print(i)# for i in range(1,20,2):# print(i)# for i in range(1,20,3):# print(i)# for i in range(20, 1, -1):# print(i)# l1 = ['alex', 'wusir', 'wusir', 100, '女神']## for i in l1:# print(l1.index(i))# 利用for range 打印出列表的索引。# for i in range(len(l1)):# print(i)# 键值对出现,Key:value 字典的key唯一重复,可以存储大量的关系型数据。# 字典3.5版本包括3.5之前是无序的。3.6之后有序。首次存入数据的顺序# 字典的key:str,int,(bool值,tuple不常用) 不可变的数据类型(可哈希的)# 字典的value:任意数据类型。# dic = {'name_list':['张三', '李四'],# 'alex': {'sex':'男','爱好': '非男'},# }# print(dic)dic = {'name': 'TaiBai', 'age': 25, 'sex': '男'}# 增:# 按照键值对 有则改之,无则添加# dic['job'] = 'Teacher'# dic['name'] = 'alex'# setdeafult# dic.setdefault('job') #无则添加,有则不变。# dic.setdefault('job','Teacher') # setdefault setdefault setdefault setdefault# dic.setdefault('name','alex')# print(dic)# 删:# ret = dic.pop('name')# print(ret) # 返回值是删除的值# print(dic)# 清空# dic.clear()# print(dic)# del# del dic['name']# print(dic)# 改# dic['name'] = 'alex'# print(dic)# dic = {"name":"jin","age":18,"sex":"male"}# dic2 = {"name":"alex","weight":75}# dic2.update(dic) # 将dic 的所有键值对覆盖并添加到dic2中。dic不变。# print(dic)# print(dic2)# 查# print(dic['name'])# print(dic['name1'])# str1 = "rrrrrrrr"# str2 = str1.replace(str1[2],'x')# print(str1,str2)li = ['alex','银角','女神','egon','太白']# for i in enumerate(li):# print(i)# for index,name in enumerate(li,1):# print(index,name)# for index, name in enumerate(li, 100): # 起始位置默认是0,可更改,此为起始位置100# print(index, name)# s1 = "xx"# s2 = s1.encode("utf-8")# s3 = s1.encode("gbk")# print(s2)# print(s3)## s4 = b'\xe5\xbc\xa0\xe6\x97\xad'# s5 = s4.decode("utf-8")# print(s5)# 深浅copyl1 = ["alex", "wusir", "burry", [1, 2, 3]]# 浅copy 只是copy表层,即开辟新的内存地址,深层还是共用内存# l2 = l1.copy()# print(id(l1), id(l1[-1]))# print(id(l2), id(l1[-1]))# 深copy 全部都是新的内存地# import copy# l3 = copy.deepcopy(l1)# print(id(l1), id(l1[-1]))# print(id(l3), id(l3[-1]))## f1 = open("d:\\a.txt", encoding="utf-8", mode="r")# content = f1.read()# print(content)# f1.close()
来源:https://www.cnblogs.com/zhangnineday/p/9904917.html