A lot of computations involve processing a string one character at a time. Often they start at the beginning, select each character in turn, do something to it, and continue until the end. The pattern of processing is called traversal. One way to write a traversal is with a while statement:

index = 0
while index < len(fruit):
letter = fruit[index]
print letter
index = index + 1

This loop traverses the string and displays each letter one a line by itself.
Another way to write a traversal is with a for loop:

for char in fruit:
print char

Each time through the loop, the next character in the string is assigned to the variable char. The loop continues until no characters are left.
The following example shows how to use concatenation (string addition) and a for loop generate an abecedarian series (that is, in alphabetical order).

from Thinking in Python

最新文章

  1. wampServer图标为橙色无法启动原因之一
  2. 【转】js判断手机访问网页
  3. NYOJ题目34韩信点兵
  4. 学习mongo系列(十一)关系
  5. 为什么高手离不了Linux系统?这就是我的理由
  6. UIKit&#160;框架之UISearchController
  7. bom type:Phantom
  8. UVAlive11324 The Largest Clique(scc+dp)
  9. C#操作Excel总结
  10. 头皮发麻的HTML课时一
  11. MQTT——订阅报文
  12. Mysql服务器SQL模式 (官方精译)
  13. 关于SELECT LAST_INSERT_ID()的使用规则
  14. LOJ #6285 分块入门9
  15. 纯C:url base64
  16. C# WinForm 菜单项的大小、高宽的手动控制
  17. Swift 实践篇之链式 UI 代码
  18. C#之#if #endif的简单用法
  19. 解决eclipse快捷键Ctrl+Alt+Down冲突问题办法
  20. Tsung测试之配置文件

热门文章

  1. 国家人工智能(AI)的美好前景
  2. OpenCV基础篇之读取显示图片
  3. Oracle 12c agent install for windows
  4. Oracle 学习笔记 13 -- 控制用户权限
  5. Process Monitor
  6. javascript系列-class10.DOM(下)
  7. Spark SQL概念学习系列之Spark SQL概述
  8. 访问Storm ui界面,出现Nimbus Summary或Supervisor Summary时有时无的问题解决(图文详解)
  9. Android ViewPager+属性动画 实现炫酷视差动画效果
  10. 预测一下web前端未来的6个趋势