用python编写计数程序

用python编写计数程序,python,Python,我不熟悉python和一般的编码,我需要帮助解决这个问题 编写一个以字符串作为输入并执行以下功能的程序: ▪ 打印字符串中的空格数 ▪ 打印小写字母的数字计数 ▪ 打印标点符号的数量 演示如何找到字符串中的最后一个空格 谢谢在字符串中的字符上循环: for char in my_string: # test if char is a space and if it succeeds, increment something # do the same for your other

我不熟悉python和一般的编码,我需要帮助解决这个问题

编写一个以字符串作为输入并执行以下功能的程序:

▪ 打印字符串中的空格数

▪ 打印小写字母的数字计数

▪ 打印标点符号的数量

演示如何找到字符串中的最后一个空格


谢谢

在字符串中的字符上循环:

for char in my_string:
    # test if char is a space and if it succeeds, increment something
    # do the same for your other tests
    pass

有一些常数可能对您有用;特别是:,和。您可以使用来查看角色是否在这些角色集中。

我将向您展示一个示例,为您提供一些想法供您使用,并将其他示例留作练习:

打印小写字母的数字计数

你可以使用过滤器和透镜一起计算东西。例如:

>>> import string
>>> s="This char -- or that one -- It's a Space."
>>> for k in [string.uppercase, string.lowercase, string.whitespace, string.punctuation]:
...     len(filter(lambda x: x in k, s))
... 
3
23
9
6
>>> string.whitespace
'\t\n\x0b\x0c\r '
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
注意,string.uppercase、string.lowercase等值是在string模块中定义的,可以在导入string模块后使用。每一个都是一个字符串值;例如:

>>> import string
>>> s="This char -- or that one -- It's a Space."
>>> for k in [string.uppercase, string.lowercase, string.whitespace, string.punctuation]:
...     len(filter(lambda x: x in k, s))
... 
3
23
9
6
>>> string.whitespace
'\t\n\x0b\x0c\r '
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

注意,在上面,>>>>是python解释器的主要提示,并且。。。是对缩进行的辅助提示。

您卡在哪里?如果您提供一些示例代码并描述您遇到的问题,您将得到更多帮助。这听起来更像是您想要上面列出的一个工作示例,然后是一个您的问题。书籍、谷歌、必应和其他在线资源是你的朋友。如果你把堆栈作为你的第一个资源,带着一个听起来像为我做你不会得到任何质量反馈的问题,来堆栈一个实际的问题,人们会很乐意帮助你。这是你希望顺利通过的课程的偶然机会吗?需要有人帮你做这项工作吗?你也可以在我的右边为x写sumx.islower,但我个人发现bool是int的一个子类,这在python中是一个难看的折痕,我不喜欢依赖它。我看到了,这些string.uppercase等函数已经内置在python中,不需要定义了?酷,谢谢我感谢你的解释和帮助。
a=input("type strint :")
space=" "
print(a.count(space))
lower=0
for w in a:
    if w.islower()==True:
        lower+=1
print(lower)
punc='!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'    
pmark=0
for p in a:
    if p in punc:
        pmark+=1
print(pmark)

# Demonstrate how you would find the last space in a string
if a[-1]== space:
    print("last space yes")