a=input("Enter the string paragraph:")
count=0
for i in a:
if i==" ":
count=count+1
print("Number of spaces in a string:",count)
Count the number of spacing logical program is working
Lambda functions, along with sum() and map() can achieve this particular task of counting the total occurrences of particular element in a string. This uses sum() to sum up all the occurrences obtained using map() .
To count the spaces in a string:Use the split() method to split the string on each space. Access the length property on the array and subtract 1. The result will be the number of spaces in the string.
Use set() method to remove a duplicate and to give a set of unique words. Iterate over the set and use count function (i.e. string. count(newstring[iteration])) to find the frequency of word at each iteration.
>>> a=input("Enter the value ")
Enter the value "My Testing String"
>>> a.count(' ')
2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With