Python: Get the first character of the first string in a list?

后端 未结 4 746
不知归路
不知归路 2020-12-23 10:38

How would I get the first character from the first string in a list in Python?

It seems that I could use mylist[0][1:] but that does not give me the f

4条回答
  •  情书的邮戳
    2020-12-23 11:44

    You almost had it right. The simplest way is

    mylist[0][0]   # get the first character from the first item in the list
    

    but

    mylist[0][:1]  # get up to the first character in the first item in the list
    

    would also work.

    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.

提交回复
热议问题