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,我们学习了如何从任何可迭代对象中统计元素。我们还学习了如何使用字典哈希来统计可迭代对象中每个元素的频率。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP