一、字符串的基本操作
基本操作
msg = "hEllo world!" print(msg.capitalize()) #首字母大写,后面的小写 Hello world! print(msg.count('l',3,10)) #查找3到10位有字母'l'的个数;最后一位可以用'-1'表示 2 print(msg.center(20,'*')) #字符串长度为20,居中显示,以'*'填充空格 ****hello world!**** print(msg.swapcase()) #大写全都变成小写,小写变大写 print(msg.endswith('d')) #判断是否以'd'结尾 False print(msg.index('o')) #返回查找'a'的索引,只返回第一个查到的位置 4 print(msg.find('a',1,9)) #查找'a',找不到返回'-1' -1 print(msg.isdigit()) #是否整数 False print(msg.islower()) #是否都是小写 print(msg.isupper()) #是否都大写 print(msg.isalnum()) #是否包含数字 print(msg.isalpha())#是否都是字母组成,不包括空格,其他符号 print(msg.isidentifier())#是否都是字母组成,同上 print(msg.istitle()) #每个单词首字母是否大写 print(msg.isspace()) #是否是空格 print(msg.isprintable()) #字符串中所有字符是否都属于可见字符
msg2=" hello " print(msg2.lstrip()) #去掉左边的空格,rstrip去掉右边的空格 print(msg2.ljust(10,'*')) #左对齐,右边以'0'填充 hello ** print(msg2.rjust(10,'0')) #右对齐,左边以'0'填充 00 hello print(msg2.zfill(10)) #用'0'填充,可以用上一种方法表示 msg3="My name is yolanda" trans=str.maketrans('yolanda','teacher') #注意两个字符串一样长, print(msg3.translate(trans)) #把句子中对应的字符串都变了 Mt hrme is tearher
字符串的format操作
>>> msg = "my name is {}, and age is {}" >>> msg.format("yolanda",18) 'my name is yolanda, and age is 18' >>> msg = "my name is {1}, and age is {0}" >>> msg.format("yolanda",18) 'my name is 18, and age is yolanda' >>> msg = "my name is {name}, and age is {age}" >>> msg.format(age=18,name="yolanda") 'my name is yolanda, and age is 18'
二、列表
names = ['yolanda','jack','apirl','anni','yolanda','angel'] 取值: names[n]
#增加 names.append('tom') #在最后加 print(names) names.insert(1,'linda') #在索引值为1('jack')的前面插入 print(names) #删除 names.remove('jack') print(names) del names[1] print(names) print(names.pop()) #默认删除最后一个并返回 #修改 names[1]='may' print(names) #查询 print(names[1:-1]) print(names.index('yolanda')) #取元素的下标,只能取到第一个的下标 first_name=names.index('yolanda') second_name=names[first_name+1:].index('yolanda') #第二个相同字符串的下标 print(second_name+first_name+1) print(names.count('yolanda')) #查询元素的个数 names2=['rain','cat'] names.extend(names2) #合并两个列表 print(names) names.reverse() #反转 print(names) names.sort() #按照ASCII排序 print(names)
n2=names 与 n3=names.copy()不同
对names列表进行操作时,n2会与names一同改变,n3不会改变
列表enumerate的用法
for i in enumerate(names): print(i[0],i[1]) for i ,ele in enumerate(names): print(i,ele) 结果是相同的 0 yolanda 1 jack 2 apirl 3 anni 4 yolanda 5 angel
查询数字列表的和:
count=[12,34,43,2,5] print(sum(count))
列:一个购物车小程序
commodity=[ ['Iphone',8800], ['coffee',50], ['shirt',300], ['bike',2000] ] shopping_list=[] price=[] while True: salary = input("Your salary:") if salary.isdigit(): salary=int(salary) break else: continue while True: print("product list".center(40,"-")) for index,i in enumerate(commodity): print(index,".",i[0],i[1]) print("end".center(40, "-")) choice=input("What do you want to buy?") if choice.isdigit(): choice=int(choice) if choice >=0 and choice < len(commodity): p = commodity[choice] if salary >= p[1]: salary = salary - p[1] shopping_list.append(p) print("Add \033[32;1m%s\033[0m in shopping list,you have %s" % (p,salary)) else: print("Not much money!You just have %s" %salary) else: print("no this commodity") elif choice == "exit": print("You have buy :".center(40,"-")) fin = [] for i in shopping_list: fin.append(i[0]) list = set(fin) price = [] for i in shopping_list: price.append(i[1]) for item in list: print(fin.count(item), item) print("Thanks for your shopping,you have cost %s"%sum(price)) exit()
给重要字符串添加颜色:
# 各种字体颜色
print("灰白色","\033[29;1m hello \033[0m")
print("白色","\033[30;1m hello \033[0m")
print("红色","\033[31;1m hello \033[0m")
print("黄绿色","\033[32;1m hello \033[0m")
print("土黄色","\033[33;1m hello \033[0m")
print("蓝色","\033[34;1m hello \033[0m")
print("紫色","\033[35;1m hello \033[0m")
print("绿色","\033[36;1m hello \033[0m")
print("灰色","\033[37;1m hello \033[0m")

# 各种背景颜色
print("背景黑","\033[40;1m hello \033[0m")
print("背景红色:","\033[41;1m hello \033[0m")
print("背景黄绿色:","\033[42;1m hello \033[0m")
print("背景土黄色:","\033[43;1m hello \033[0m")
print("背景蓝色:","\033[44;1m hello \033[0m")
print("背景紫色:","\033[45;1m hello \033[0m")
print("背景深绿色:","\033[46;1m hello \033[0m")
print("背景灰色:","\033[47;1m hello \033[0m")

浙公网安备 33010602011771号