Python:获取列表中第一个字符串的第一个字符?
问题描述:
如何从Python列表中的第一个字符串中获取第一个字符?
How would I get the first character from the first string in a list in Python?
似乎我可以使用mylist[0][1:],但这没有给我第一个字符.
It seems that I could use mylist[0][1:] but that does not give me the first character.
>>> mylist = []
>>> mylist.append("asdf")
>>> mylist.append("jkl;")
>>> mylist[0][1:]
'sdf'
答
您几乎是正确的.最简单的方法是
You almost had it right. The simplest way is
mylist[0][0] # get the first character from the first item in the list
但是
mylist[0][:1] # get up to the first character in the first item in the list
也可以.
您要在第一个字符(零个字符)之后结束,而不是开始在第一个字符(零个字符)之后,这就是您问题中代码的含义
You want to end after the first character (character zero), not start after the first character (character zero), which is what the code in your question means.