天天看点

python中索引和下标_Python实现带下标索引的遍历操作示例

本文实例讲述了Python实现带下标索引的遍历操作。分享给大家供大家参考,具体如下:

代码如下:

#coding=utf-8

#python - 实现带下标索引的遍历.

str = 'abcdefghigklmn'

#方式一:for

i = 0

for ch in str:

print('%d\t%s'%(i,ch))

i+=1

print('-'*50)

#方式二:enumerate()

for i,ch in enumerate(str):

print i,ch

运行结果:

0   a

1   b

2   c

3   d

4   e

5   f

6   g

7   h

8   i

9   g

10  k

11  l

12  m

13  n

--------------------------------------------------

0 a

1 b

2 c

3 d

4 e

5 f

6 g

7 h

8 i

9 g

10 k

11 l

12 m

13 n

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python字符串操作技巧汇总》、《Python字典操作技巧汇总》、《Python列表(list)操作技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

本文标题: Python实现带下标索引的遍历操作示例

本文地址: http://www.cppcns.com/jiaoben/python/261155.html