将字符串生成迭代器,逐个访问字符串中每个字符,并大写合并输出。
分析字符串转换迭代器,for循环遍历字符串。
答案 # 方法一s1 = 'Python'l = []for i in iter(s1): # 使用iter()函数生成迭代器l.append(i.upper())print(''.join(l))# 方法二s1 = 'Python'l = []for i,char in enumerate(s1): # 使用enumerate()函数将字符串转换为索引序列l.append(char.upper())print(''.join(l))# 方法三s1 = 'Python'l = []for i in range(len(s1)): # 通过range()生成数字列表l.append(s1[i].upper())print(''.join(l))