Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
removing spaces
#2
As with most things, there's more than one way to do this.

One way to count the number of 'words' from a input, is to convert said into a list, which is what I think that you are attempting with...
words = len(Text.split())
I would go one stage further and create a list object with which to work. Also, you've not specified a split separator.

This code...

text_input = input("Enter you text: ")

list_input = text_input.split(' ')

words = len(list_input)

print(f"Your input contains {words} words.")
... creates a list object from the user input and then simply outputs the length of that list as the number of words contained therein. What constitutes a 'word' is open to interpretation, but as a basic concept, this may be of help to you.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Messages In This Thread
removing spaces - by msaiahnl - Jul-25-2022, 02:56 PM
RE: removing spaces - by rob101 - Jul-25-2022, 03:31 PM
RE: removing spaces - by deanhystad - Jul-25-2022, 03:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing leading\trailing spaces azizrasul 8 2,837 Oct-23-2022, 11:06 PM
Last Post: azizrasul
  removing spaces/tabs after used .strip() zarize 0 1,628 Sep-11-2019, 12:46 PM
Last Post: zarize

Forum Jump:

User Panel Messages

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