# define string variable tweet2
tweet2 = 'Want cheap snacks? Visit @cssu office in BA2283'
# find position of substring 'cheap', which is 5 (strings has 0-based indices in python
n = tweet2.find('cheap')
# print 5th element of string, which is 'c'
print(tweet2[n])
您应该考虑阅读关于字符串方法和列表的python文档
这是因为find(str,string)方法确定str是出现在string中,还是出现在string的子字符串中,并返回第一次出现的位置。因此,当你调用
tweet2.find('cheap')
时,它将返回位置,这是廉价的第一次出现。在{c>在索引的开头返回
相关问题 更多 >
编程相关推荐