Python程式用於統計字串中空格的數量
在這篇文章中,我們將介紹一個Python程式來統計字串中空格的數量。
使用的方法
以下是完成此任務的各種方法 -
使用for迴圈(帶索引)
使用count()函式
使用isspace()函式
使用Counter()函式
使用operator模組的countOf()函式
假設我們已經獲取了一個輸入字串。我們現在將使用上述方法來統計輸入字串中空格的數量。
方法1:使用for迴圈(帶索引)
演算法(步驟)
以下是執行所需任務的演算法/步驟 -。
建立一個函式countSpaces(),透過接受輸入字串作為引數來返回字串中空格的數量。
初始化一個值為0的變數來儲存空格的總數。
使用for迴圈,使用len()函式(返回物件中專案的數量)遍歷字串的長度。
使用if條件語句檢查字串的每個字元是否為空格。
如果上述條件為真,則將計數值加1
返回輸入字串中空格的數量。
建立一個變數來儲存輸入字串。
透過將輸入字串作為引數傳遞來呼叫上面定義的countSpaces()函式。
示例
以下程式使用for迴圈(帶索引)返回輸入字串中空格的數量 -
# function to return the count of no of spaces in a string # by accepting the input string as an argument def countSpaces(inputString): # storing the count of the number of spaces in a given string spaces_count = 0 # Traversing till the length of the string for index in range(0, len(inputString)): # checking whether each character of a string is blank/space or not if inputString[index] == " ": # incrementing the space value count by 1 spaces_count += 1 # returning the count of the number of spaces in an input string return spaces_count # input string inputString = "tutorialspoint is a best learning platform for coding" # calling the above defined countSpaces() function by # passing input string as an argument print("Count of no of spaces in an input string:", countSpaces(inputString))
輸出
執行上述程式將生成以下輸出 -
Count of no of spaces in an input string: 7
方法2:使用count()函式
count()函式 - 返回給定值在字串中出現的次數。
語法
string.count(value, start, end)
示例
以下程式使用count()函式返回輸入字串中空格的數量 -
# creating a function to return the count of no of spaces in a string # by accepting the input string as an argument def countSpaces(inputString): # returing the spaces count in a string using the count() function return inputString.count(" ") # input string inputString = "hello tutorialspoint python" # calling the above defined countSpaces() function by # passing input string as an argument print("Count of no of spaces in an input string:",countSpaces(inputString))
輸出
執行上述程式將生成以下輸出 -
Count of no of spaces in an input string: 2
方法3:使用isspace()函式
isspace()函式 - 如果字串中所有字元都是空格,則返回True,否則返回False。
語法
string.isspace()
示例
以下程式使用isspace()函式返回輸入字串中空格的數量 -
# input string inputString = "hello tutorialspoint python codes" # storing the count of spaces spaces_count = 0 # traversing through each character of the input string for c in inputString: # checking whether the current character is space or not using isspace() function if(c.isspace()): # incrementing the spaces_count value by 1 if the condition is true spaces_count += 1 # printing the count of no of spaces in an input string print("Count of no of spaces in an input string:", spaces_count)
輸出
執行上述程式將生成以下輸出 -
Count of no of spaces in an input string: 3
方法4:使用Counter()函式
Counter()函式 - 一個子類,用於計算可雜湊物件。它在被呼叫/呼叫時隱式地建立一個可迭代物件的雜湊表。
這裡Counter()函式將輸入字串的每個字元的頻率作為鍵值對返回。
示例
以下程式使用Counter()函式返回輸入字串中空格的數量 -
# importing a Counter function from the collections module from collections import Counter # input string inputString = "hello tutorialspoint python codes" # getting the frequency of each character of the string as a # key-value pair using Counter() function frequency = Counter(inputString) # getting the frequency/count of spaces spaces_count = frequency[' '] # printing the count of no of spaces in an input string print("Count of no of spaces in an input string:", spaces_count)
輸出
執行上述程式將生成以下輸出 -
Count of no of spaces in an input string: 3
方法5:使用operator模組的countOf()函式
示例
以下程式使用operator模組的countOf()函式返回輸入字串中空格的數量 -
# importing operator module with alias name op import operator as op # creating a function to return the count of no of spaces in a string # by accepting the input string as an argument def countSpaces(inputString): # returing the spaces count in a string using the countOf() function return op.countOf(inputString, " ") # input string inputString = "hello tutorialspoint python" # calling the above defined countSpaces() function by # passing input string as an argument print("Count of no of spaces in an input string:", countSpaces(inputString))
輸出
執行上述程式將生成以下輸出 -
Count of no of spaces in an input string: 2
結論
在本文中,我們介紹了5種不同的方法來統計字串中空格的數量。使用新的運算子函式countOf,我們學習瞭如何從任何可迭代物件中統計元素。我們還學習瞭如何使用字典雜湊來統計可迭代物件中每個元素的頻率。