Python 显示字符串中小写字母的数量
这就是我到目前为止所做的:Python 显示字符串中小写字母的数量,python,python-3.x,Python,Python 3.x,这就是我到目前为止所做的: count=0 mystring=input("enter") for ch in mystring: if mystring.lower(): count+=1 print(count) 我知道了如何制作一个程序来显示字符串中小写字母的数量,但它要求我单独列出每个字母:如果ch='a'或ch='b'或ch='c',等等。我试图知道如何使用命令来实现这一点。您的代码中有一个打字错误。而不是: if my.string.lower(): 应该
count=0
mystring=input("enter")
for ch in mystring:
if mystring.lower():
count+=1
print(count)
我知道了如何制作一个程序来显示字符串中小写字母的数量,但它要求我单独列出每个字母:
如果ch='a'或ch='b'或ch='c'
,等等。我试图知道如何使用命令来实现这一点。您的代码中有一个打字错误。而不是:
if my.string.lower():
应该是:
if ch.islower():
如果您有任何问题,请在下面提问。祝你好运 您的代码中有一个输入错误。而不是:
if my.string.lower():
应该是:
if ch.islower():
如果您有任何问题,请在下面提问。祝你好运 我不确定这是否能很好地处理UTF或特殊字符,但至少在Python3中可以使用
islower()
函数处理ASCII
count=0
mystring=input("enter:")
for ch in mystring:
if ch.islower():
count+=1
print(count)
我不确定这是否能很好地处理UTF或特殊字符,但至少能在Python3中使用
islower()
函数处理ASCII
count=0
mystring=input("enter:")
for ch in mystring:
if ch.islower():
count+=1
print(count)
您的代码的正确版本为:
count=0
mystring=input("enter")
for ch in mystring:
if ch.islower():
count += 1
print(count)
方法lower
将字符串/字符转换为小写。在这里,您想知道它是否是小写的(您想要一个布尔值),因此需要islower
提示:通过一些技巧,您甚至可以编写以下内容:
mystring= input("enter")
count = sum(map(lambda x: x.islower(), mystring))
或
(True
自动转换为1
,False
转换为0
)
:)正确的代码版本是:
count=0
mystring=input("enter")
for ch in mystring:
if ch.islower():
count += 1
print(count)
方法lower
将字符串/字符转换为小写。在这里,您想知道它是否是小写的(您想要一个布尔值),因此需要islower
提示:通过一些技巧,您甚至可以编写以下内容:
mystring= input("enter")
count = sum(map(lambda x: x.islower(), mystring))
或
(True
自动转换为1
,False
转换为0
)
:)这听起来像是家庭作业!安威,这是一种有趣的方式:
#the operator module contains functions that can be used like
#their operator counter parts. The eq function works like the
#'=' operator; it takes two arguments and test them for equality.
from operator import eq
#I want to give a warning about the input function. In python2
#the equivalent function is called raw_input. python2's input
#function is very different, and in this case would require you
#to add quotes around strings. I mention this in case you have
#been manually adding quotes if you are testing in both 2 and 3.
mystring = input('enter')
#So what this line below does is a little different in python 2 vs 3,
#but comes to the same result in each.
#First, map is a function that takes a function as its first argument,
#and applies that to each element of the rest of the arguments, which
#are all sequences. Since eq is a function of two arguments, you can
#use map to apply it to the corresponding elements in two sequences.
#in python2, map returns a list of the elements. In python3, map
#returns a map object, which uses a 'lazy' evaluation of the function
#you give on the sequence elements. This means that the function isn't
#actually used until each item of the result is needed. The 'sum' function
#takes a sequence of values and adds them up. The results of eq are all
#True or False, which are really just special names for 1 and 0 respectively.
#Adding them up is the same as adding up a sequence of 1s and 0s.
#so, map is using eq to check each element of two strings (i.e. each letter)
#for equality. mystring.lower() is a copy of mystring with all the letters
#lowercase. sum adds up all the Trues to get the answer you want.
sum(map(eq, mystring, mystring.lower()))
或一行:
#What I am doing here is using a generator expression.
#I think reading it is the best way to understand what is happening.
#For every letter in the input string, check if it is lower, and pass
#that result to sum. sum sees this like any other sequence, but this sequence
#is also 'lazy,' each element is generated as you need it, and it isn't
#stored anywhere. The results are just given to sum.
sum(c.islower() for c in input('enter: '))
这听起来像是家庭作业!安威,这是一种有趣的方式:
#the operator module contains functions that can be used like
#their operator counter parts. The eq function works like the
#'=' operator; it takes two arguments and test them for equality.
from operator import eq
#I want to give a warning about the input function. In python2
#the equivalent function is called raw_input. python2's input
#function is very different, and in this case would require you
#to add quotes around strings. I mention this in case you have
#been manually adding quotes if you are testing in both 2 and 3.
mystring = input('enter')
#So what this line below does is a little different in python 2 vs 3,
#but comes to the same result in each.
#First, map is a function that takes a function as its first argument,
#and applies that to each element of the rest of the arguments, which
#are all sequences. Since eq is a function of two arguments, you can
#use map to apply it to the corresponding elements in two sequences.
#in python2, map returns a list of the elements. In python3, map
#returns a map object, which uses a 'lazy' evaluation of the function
#you give on the sequence elements. This means that the function isn't
#actually used until each item of the result is needed. The 'sum' function
#takes a sequence of values and adds them up. The results of eq are all
#True or False, which are really just special names for 1 and 0 respectively.
#Adding them up is the same as adding up a sequence of 1s and 0s.
#so, map is using eq to check each element of two strings (i.e. each letter)
#for equality. mystring.lower() is a copy of mystring with all the letters
#lowercase. sum adds up all the Trues to get the answer you want.
sum(map(eq, mystring, mystring.lower()))
或一行:
#What I am doing here is using a generator expression.
#I think reading it is the best way to understand what is happening.
#For every letter in the input string, check if it is lower, and pass
#that result to sum. sum sees this like any other sequence, but this sequence
#is also 'lazy,' each element is generated as you need it, and it isn't
#stored anywhere. The results are just given to sum.
sum(c.islower() for c in input('enter: '))
我认为你可以使用以下方法:
mystring=input("enter:")
[char.lower() for char in mystring].count( True ) )
我认为你可以使用以下方法:
mystring=input("enter:")
[char.lower() for char in mystring].count( True ) )
ch.lower()
将始终为True
,因为ch.lower()
是ch
的小写版本,非空字符串测试为True
。我认为OP需要if ch.islower():
来代替。谢谢!我不知道“.islower”方法。没问题,很乐意帮忙ch.lower()
总是True
,因为ch.lower()
是ch
的小写版本,非空字符串测试为True
。我认为OP需要if ch.islower():
来代替。谢谢!我不知道“.islower”方法。没问题,很乐意帮忙!是什么阻止了你打印每个ch
?没有什么真正的原因,除了它太耗时。是什么阻止了你打印每个ch
?没有什么真正的原因,除了它太耗时。