首页 > 技术文章 > TypeError: string indices must be integers, not str

hanframe 2014-07-01 16:46 原文

1. TypeError: string indices must be integers, not str

字符串类型取第index个字符的时候,应该传入int而不是str。如

 
1 a='abcdef'
2 print a[0]
3 #而不是 print a['0']

更常见的情况是把一个string当成了字典在使用 :should_be_dict_but_string['id'] 。这样的错误

推荐阅读