Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with string practice
#1
Hi everyone,
I'm looking for some help with an exercise I was doing, regarding strings in python.
This program is supposed to find the longest word out of a list of words the user types (i.e. if I type "chicken, dog and cat" the output will be "chicken"), but for the sake of a possibility, if I did type, for example, only "dog and cat", the output will always be dog. Thing is, I want my program to print them both.
Here is the code:

def longest_word(l):
    lenl=[]
    for n in l:
        lenl.append((len(n), n))
    lenl.sort()
    return(lenl[-1][1])
lis=input("Insert some words ").replace(",", " ").replace("and", " ")
lispar=lis.split(" ")
print(longest_word(lispar))
Thanks in advance! ( Oh, and if you find anything I could improve here, furthermore, just let me know, it's very appreciated Tongue )
Reply


Messages In This Thread
Help with string practice - by Hermann_Fegelein - Aug-15-2018, 02:36 PM
RE: Help with string practice - by ichabod801 - Aug-15-2018, 03:39 PM
RE: Help with string practice - by Hermann_Fegelein - Aug-15-2018, 04:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Best practice on using virtual environments in Python bytecrunch 6 949 Feb-14-2024, 03:22 PM
Last Post: snippsat
  best practice for import libraries and using pyinstaller aster 3 2,928 Apr-17-2021, 11:12 AM
Last Post: snippsat
  Threading best practice EvanS1 2 1,972 Apr-21-2020, 10:11 PM
Last Post: EvanS1
  How to properly close db connection, best practice t4keheart 6 3,096 Jan-24-2020, 06:58 PM
Last Post: Marbelous
  Best Practice For String Quotations ? Zork_3 9 50,071 Sep-01-2017, 07:16 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020