【发布时间】:2010-10-01 01:02:58
【问题描述】:
我有一个字符串'Hello',我需要找出哪些字符占用了哪些索引。
伪代码:
string = 'Hello'
a = string.index(0)
b = string.index(4)
print a , b
a 是“H”,b 是“o”。
【问题讨论】:
-
你用的是什么教程?你在哪一章?
标签: python
我有一个字符串'Hello',我需要找出哪些字符占用了哪些索引。
伪代码:
string = 'Hello'
a = string.index(0)
b = string.index(4)
print a , b
a 是“H”,b 是“o”。
【问题讨论】:
标签: python
a = "Hello"
print a[0]
print a[4]
【讨论】:
Python 中的字符串 (str) 是 sequence type,因此可以使用 [] 访问:
my_string = 'Hello'
a = my_string[0]
b = my_string[4]
print a, b # Prints H o
这意味着它还支持切片,这是在 Python 中获取子字符串的标准方式:
print my_string[1:3] # Prints el
【讨论】:
string。
我认为他是在要求这个
for index,letter in enumerate('Hello'):
print 'Index Position ',index, 'The Letter ', letter
也许我们想探索一些数据结构
所以让我们添加一个字典 - 我可以懒惰地这样做,因为我知道索引值是唯一的
index_of_letters={}
for index,letter in enumerate('Hello'):
index_of_letters[index]=letter
index_of_letters
【讨论】: