Slicingstrings
In python, I want to slice strings in a list in such a way that first few characters from that list must be removed/spliced out.
a=['hello', 'things', 'becoming', 'expensive']
如何从列表中的每个字符串中删除前两个字符以获取输出
['llo', 'ings', 'coming', 'pensive']
回答
a=['hello', 'things', 'becoming', 'expensive']
b = []
for word in a:
b.append(word[2:])
print(b)
结果是:
['llo', 'ings', 'coming', 'pensive']
这段:
word[2:]
是说从索引 2 开始并将所有内容返回到它的右侧。索引从 0 开始,所以前 2 个字母被忽略