1 my_str = 'hello' 2 # for循环 3 for v in my_str: 4 print(v) 5 # while 配合迭代器实现字符串的遍历 6 ite = iter(my_str) 7 while True: 8 try: 9 each = next(ite) 10 except StopIteration: 11 break 12 print(each) 13 14 results: 15 h 16 e 17 l 18 l 19 o 20 h 21 e 22 l 23 l 24 o