python

Python中怎么计算字符串中特定字符出现的次数

小亿
84
2024-04-03 09:47:21
栏目: 编程语言

可以使用count()方法来计算字符串中特定字符出现的次数。例如:

string = "Hello, World!"
char = "l"

count = string.count(char)
print(f"The character '{char}' appears {count} times in the string.")

这将输出:

The character 'l' appears 3 times in the string.

0
看了该问题的人还看了