编写python程序,统计一段英文段落中所有字符出现的次数,要求输出9个统计项后换行。
时间: 2023-06-04 15:02:27 浏览: 271
好的,我可以帮你解决这个问题。
以下是 Python 代码示例:
```python
import string
paragraph = "This is a paragraph containing some English words and characters. It has uppercase and lowercase letters, digits, punctuation marks, and even some whitespaces."
# Convert all characters to lowercase for accurate counting
paragraph = paragraph.lower()
# Define a dictionary to hold the count of each character
char_count = {}
# Loop through the paragraph and count each character
for char in paragraph:
# Only count alphanumeric characters and punctuation marks
if char in string.ascii_letters + string.digits + string.punctuation:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
# Print the count of all characters
for char, count in char_count.items():
print(char, count)
# Print a newline after the output
print()
```
运行以上代码,你将得到字符计数的输出结果,每行包含一个字符和该字符在段落中出现的次数。
希望这能帮到你。
阅读全文