Searching For A Title Thats 'starts With' In Python?
So I have a list of names name_list = ['John Smith', 'John Wrinkle', 'John Wayne', 'David John', 'David Wrinkle', 'David Wayne'] I want to be able to search, for example, John and
Solution 1:
Change your matches
to:
matches = [name for name in name_list if name.startswith(search)]
You can also make some changes to your code:
# You can do this in one go
search = input(str("Search: ")).lower()
# Why bother looping if search string wasn't provided.ifnot search:
print("Empty search field")
else:
# This can be a generatorfor i in (name for name in name_list if name.startswith(search)):
print(i.title())
Post a Comment for "Searching For A Title Thats 'starts With' In Python?"