Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count number of space in given string in python [duplicate]

Tags:

python

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

like image 996
Myscript Avatar asked Nov 23 '15 05:11

Myscript


People also ask

How do you count the number of repeated elements in a string in Python?

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() .

How do you count the number of spaces in a string?

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.

How do you count the number of repeated words in Python?

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.


1 Answers

>>> a=input("Enter the value ")
Enter the value "My Testing String"
>>> a.count(' ')
2
like image 56
Mayur Koshti Avatar answered Sep 21 '22 09:09

Mayur Koshti