python3----基礎 用while循環+iter()+next() 實現對字符串的遍歷與輸出


 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM