Skip to Main Content
Blog

Count Words in Python

Count Words in Python

def main():
	"""
	Counts the number of words in a string.
	"""

	text = input("Enter string: ")

	count = 1
	for i in range(1, len(text)):
		a = text[i - 1]
		b = text[i]
		if a.isspace() and not b.isspace():
			count += 1

	print(count)


if __name__ == "__main__":
	main()