0
Longest word - Can i solve them with functions? Can anyone help?
I am stuck in a question and tried many times but can't solve this out. Look what i did, txt = input() def longest() return longest longest(txt.split(' '))
5 odpowiedzi
+ 1
Hi Haris,
Python has in the max method an optional argument key which can be used to test on the length. It works for Strings on lists, so you need to split your string into a list.
e.g.: print(max(txt.split(), key = len))
where txt is the variable with the full string.
+ 1
Julian Zimpel
txt = input()
print(max(txt.split(' '), key = len))
but they didn't told about max that contains key method but okay.
I Got it, thank you!
+ 1
since you already learned about max, and while i am big fan of loops, here a nice code to search for longest word i just wrote( you can consider it a bita version) simple and easy, give it a try :)
txt= input()
lst_txt=txt.split(" ")
max=0
for word in lst_txt:
if len(word)>max:
max=len(word)
longest=word
print(longest)
0
Ali M thank you ali 👍🇵🇸 falastini
0
you are so welcomed, my brother 🤗