此 Python 字符串函数在指定的开始和结束位置计算子字符串出现的次数并返回。此处开始和结束索引位置是可选的。本节讨论如何通过多个示例编写字符串 count 函数。
此 count 函数返回一个整数值。例如,如果我们说 ‘Hello World’.count(‘l’, 0, 7),它会开始在 ‘Hello World’ 中查找 l,并返回两个,因为结束值的索引位置是 7(即,r)。Python String Count 函数的语法如下所示。
String_Value.count(Substring, Starting_Position, Ending_Position)
- 子字符串:您想在其上使用该函数的字符串,此参数是必需的。count 函数在 String_Value 中查找此子字符串,如果找到文本,则返回结果。
- 起始位置:这是一个可选参数。如果您想指定起始点(起始索引位置),请在此处指定。例如,从字符串中间或特定索引开始计数。如果省略此参数,Python String count 函数将零视为起始位置。
- 结束位置(可选):如果您想指定终点(结束索引位置),请在此处指定。例如,查找字符串中间或特定索引之前。如果省略此参数,则认为它是最高数字。
Python 字符串 count 示例
count 函数对于计算子字符串在给定字符串中重复的次数很有用。以下示例集可帮助您理解字符串 count 函数。
Str1 = 'We are abc working at abc company with abc Employee';
Str2 = Str1.count('abc')
print('First Output of this method is = ', Str2)
# Performing directly
Str3 = 'Find Tutorial at Tutorial Gateway'.count('Tutorial')
print('Second Output of a method is = ', Str3)
# Using First Index
Str5 = Str1.count('abc', 12)
print('Third Output of a method is = ', Str5)
# Using First & Second
Str6 = Str1.count('abc', 12, len(Str1) -1)
print('Fourth Output of a method is = ', Str6)
# Using First & Second
Str7 = Str1.count('abc', 12, 21)
print('Fifth Output of a method is = ', Str7)

首先,我们声明了 String 变量 Str1 并为其分配了数据。
Str1 = 'We are abc working at abc company with abc Employee';
以下语句使用此 Python 函数计算子字符串 ‘abc’ 在字符串 Str1 中重复的次数,并打印输出。
Str2 = Str1.count('abc')
它还允许我们使用起始索引位置。通过指定它,我们可以提高 字符串函数 的性能。
Str5 = Str1.count('abc', 12)
它允许我们使用起始和结束索引。通过指定起始和结束,我们可以提高性能。下面的语句 count 返回从索引位置 12 到末尾的 abc 字符串的出现次数。
Str6 = Str1.count('abc', 12, len(Str1) -1)
它返回零,因为此函数从 12 开始查找(这意味着跳过了第一个 abc),并在索引位置 21 结束。正如我们所知,第二个 abc 在 22。这意味着在这些之间没有需要计数的子字符串。
Str7 = Str1.count('abc', 12, 21)
count 函数示例 2
以下示例集可帮助您了解此 Python 编程语言中的字符串 count 函数。在此 Python 示例中,首先,我们声明了两个 String 变量 Str1 和 Str2,并分配了相应的值。
第一个语句查找 ‘a’ 在 Str1 字符串中重复的次数并打印输出。下面的 count 语句将 Str1 字符串从 6 开始切片到 25。在切片后的字符串中,它查找 ‘a’ 字符串将重复的次数并打印输出。
对于 Str5,它将 Str1 字符串从 6 开始切片到末尾(要查找末尾值,我们使用了 len 函数)。在切片后的字符串中,计算 ‘a’ 将重复的次数。
在最后一个语句 Str6 中,我们直接在字符串上使用了该函数。
Str1 = 'learn Python at tutorial gateway'
Str2 = 'a'
Str3 = Str1.count(Str2)
print("Total Number of a's in String1 = ", Str3)
Str4 = Str1.count(Str2, 6, 25)
print("Total Number of a's in Sliced String1 = ", Str4)
Str5 = Str1.count(Str2, 6, len(Str1))
print("Total Number of a's in Sliced String1 = ", Str5)
# Performing directly
Str6 = 'learn Python at tutorial gateway'.count('a', 0, 60)
print("Total Number of a's in String1 = ", Str6)

Python 字符串 count 函数的替代方法
从面试的角度来看,他们可能会要求您在不使用内置方法的情况下显示字母计数。在这里,我们使用 for 循环来迭代字符串中的字符。if 语句比较字符并增加计数器。
Str1 = 'learn Python at tutorial gateway'
Str2 = 'a'
co = 0
for char in Str1:
if char == Str2:
co = co + 1
print("Total Number of a's in String1 = ", co)
Total Number of a's in String1 = 5
在此程序中,我们首先使用 For 循环 来迭代 Str1 中的字符。在 for 循环中,我们使用 If 语句 来检查每个字符串字符是否等于 Str2。如果为 True,则 cn 值增加 1。最后的 print 语句打印输出。
以下示例是此 count 函数的另一种替代方法。
from collections import Counter
Str1 = 'learn Python at tutorial gateway'
Str2 = 'a'
cn = Counter(Str1)[Str2]
print("Total Number of a's in String1 = ", cn)
Total Number of a's in String1 = 5