str1='王者荣耀'
#直接遍历:
for i in str1:
print(i)
#间接遍历:
for i in range(len(str1)):
print(str1[i])
#获取“王荣”
for i in range(0,len(str1),2)#→修改默认步长,起始值必写
print(str1[i])
5.2 练习2:“abcderf”→“ABCDERF"
print(ord('a'), ord('A')) # a→A,十进制:97→65,相差32
str2 = "abcderf"
str3 = ''
for i in str1:
str2 += chr(ord(i) - 32)
# str2=str2+chr(ord(i) - 32)
print(str2)
5.3 练习3:“abcdABCD一二三四”→“ABCDabcd一二三四"
str4=“abcdABCD一二三四”
str5=''
for i in str4:
#字符串中第一种类型元素(小写字母)满足条件
if'a'<=i<='z':
#可直接写十进制代码值,
#if 97<=ord(i)<=122:
str5+=chr(ord(i)-32)
#字符串中第二种类型元素(大写字母)满足条件
elsif'A'<=i<='Z':
#可直接写十进制代码值,
#if 65<=ord(i)<=90:
str5+=chr(ord(i)+32)
else:
#字符串中第三种类型元素(汉字)满足条件
str5+=i
print(str5)
str1 = "你可真是个垃圾"
list1 = ['你', '可', '真是', '个', '垃圾']
stop_words = ['垃圾', '菜鸡', '辣鸡']
str2 = ''
for i in list1:
if i in stop_words:
str2 += str1.replace(i,'*'*len(i))
print('屏蔽不文明用语之后的结果:',str2)
`` str1 = “你可真是个垃圾” list1 = [‘你’, ‘可’, ‘真是’, ‘个’, ‘垃圾’] stop_words = [‘垃圾’, ‘菜鸡’, ‘辣鸡’] str2 = ‘’ for i in list1: if i in stop_words: str2 += str1.replace(i,‘*’*len(i)) print(‘屏蔽不文明用语之后的结果:’,str2)