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()函数
例子
以下程序使用操作符模块的 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,我们学习了如何对任何可迭代对象中的元素进行计数。我们还学习了如何使用字典哈希来计算可迭代的每个元素的频率。