Object 'str' does not have attribute 'append'

advertisements

I'm having all sorts of trouble with this Python code, not good at coding, but have gone this far:

students = input("Students: ")
print('Class Roll')
myList = students.append()
myList.sort()
print(students[0])
print(students[1])
print(students[2])
print(students[3])
print(students[4])

List that it has to sort in order is: Peng Ivan Alan Jodi Macy

It comes back with this:
Traceback (most recent call last):
  File "program.py", line 12, in <module>
    myList = students.append()
AttributeError: 'str' object has no attribute 'append'

Please help in easy to understand language, I need to have this right to progress onto the next round of code.


Have a look at the Official Python Tutorial to begin with.

myList = students.append()

In this line, you are basically trying to create a list myList by appending nothing (since the parameter list is empty) to a string students.

The following code is probably what you need as far as you have described in your question:

students = input("Students: ")

myList = students.split()
myList.sort()

print('Class Roll')
for student in myList:
    print(student)