Python:获取列表中第一个字符串的第一个字符? [英] Python: Get the first character of the first string in a list?

查看:3172
本文介绍了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.

这篇关于Python:获取列表中第一个字符串的第一个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆