0

Number of words in a string in python

I want to write a code that will take the number of words in a string. The thing is between two words may be more than one space. Can anyone help me?

1st Sep 2021, 6:19 PM
Yasaman Shokri
Yasaman Shokri - avatar
2 Antworten
+ 9
take in the string using input() and split it using: lst = your_text.split() do not use anything as argument in split(). this handles multiple consecutive spaces as one space. the splitted words will be stored in a list. to get the number of words, use len() function. happy coding and good success!
1st Sep 2021, 6:25 PM
Lothar
Lothar - avatar
+ 3
Your question is not entirely clear to me. If you want to get the first and last words from a string, you can do it with the following way. >>> s = input() # example Get the first and last words >>> s = s.split() >>> first, last = s[0], s[-1] >>> first 'Get' >>> last 'words'
1st Sep 2021, 7:14 PM
Operand
Operand - avatar