【问题标题】:capitalising the first letter in a list将列表中的第一个字母大写
【发布时间】:2017-12-17 13:47:10
【问题描述】:

我正在编写一个程序,该程序获取学生姓名列表并对它们进行排序以创建班级名册。名称列表将在一行中给出,由一个空格分隔。

我的程序应该是这样工作的:

学生:彭伊万·艾伦·乔迪·梅西
班级卷
艾伦
伊万
乔迪
梅西百货

学生的名字总是大写(第一个字母大写,名字的其余部分小写),并按字母顺序排列 到目前为止我有:

data = input("Students: ")
print('Class Roll')
data.sort()
for s in data:
    print(s)

但是上面说sort不是str的属性,那我该怎么办呢?

【问题讨论】:

  • 您的下一行是for s in data。你认为data 是什么类型的?
  • 我的回答对你有帮助还是你还需要什么?

标签: python list capitalization


【解决方案1】:

使用input,您会得到一个字符串。

所以要得到一个列表,你需要拆分字符串:

data = input("Students: ")
students = data.split(' ')
result = sorted([s.title() for s in students])

【讨论】:

    猜你喜欢
    • 2017-04-17
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    相关资源
    最近更新 更多