Ron
Ron

Reputation: 105

AttributeError: 'str' object has no attribute 'sort'

The Task Reads...

Write a program that takes a list of student names and sorts them to create a class roll. The list of names will be given on a one line separated by a single space.

So I have my code.

items=input("Students: ")
items.sort(lambda x, y: cmp(x.lower(),y.lower()))
print(items)

Why am i getting this, "AttributeError: 'str' object has no attribute 'sort'" Error"

Cheer's In Advanced

Ronny

Upvotes: 3

Views: 25800

Answers (1)

TerryA
TerryA

Reputation: 60004

input() returns a string. If you would like for items to be a list, you can do item.split():

Let's assume items is John Mary Bill

You can then do:

items = items.split()

Then do items.sort(), as items will be a list object, not a string.

Upvotes: 9

Related Questions