如何在Python中检测小写字母?

时间:2021-12-11 05:40:49

I need to know if there is a function that detects the lowercase letters in a string. Say I started writing this program:

我需要知道是否有一个函数可以检测字符串中的小写字母。说我开始编写这个程序:

s = input('Type a word')

Would there be a function that lets me detect a lowercase letter within the string s? Possibly ending up with assigning those letters to a different variable, or just printing the lowercase letters or number of lowercase letters.

是否有一个函数可以让我检测字符串s中的小写字母?可能最终将这些字母分配给不同的变量,或者只打印小写字母或小写字母数。

While those would be what I would like to do with it I'm most interested in how to detect the presence of lowercase letters. The simplest methods would be welcome, I am only in an introductory python course so my teacher wouldn't want to see complex solutions when I take my midterm. Thanks for the help!

虽然那些是我想用它做的,但我最感兴趣的是如何检测小写字母的存在。最简单的方法是受欢迎的,我只是在一个介绍性的python课程,所以我的老师不希望看到复杂的解决方案,当我采取我的期中考试。谢谢您的帮助!

6 个解决方案

#1


29  

To check if a character is lower case, use the islower method of str. This simple imperative program prints all the lowercase letters in your string:

要检查字符是否为小写,请使用str的islower方法。这个简单的命令式程序打印字符串中的所有小写字母:

for c in s:
    if c.islower():
         print c

Note that in Python 3 you should use print(c) instead of print c.

请注意,在Python 3中,您应该使用print(c)而不是print c。


Possibly ending up with assigning those letters to a different variable.

可能最终将这些字母分配给不同的变量。

To do this I would suggest using a list comprehension, though you may not have covered this yet in your course:

要做到这一点,我建议使用列表理解,虽然你可能还没有在你的课程中涵盖这个:

>>> s = 'abCd'
>>> lowercase_letters = [c for c in s if c.islower()]
>>> print lowercase_letters
['a', 'b', 'd']

Or to get a string you can use ''.join with a generator:

或者要获得一个字符串,你可以使用'.inin和一个生成器:

>>> lowercase_letters = ''.join(c for c in s if c.islower())
>>> print lowercase_letters
'abd'

#2


10  

You can use built-in function any and generator.

您可以使用内置函数any和generator。

>>> any(c.islower() for c in 'Word')
True

>>> any(c.islower() for c in 'WORD')
False

#3


10  

There are 2 different ways you can look for lowercase characters:

您可以通过两种不同的方式查找小写字符:

  1. Use str.islower() to find lowercase characters. Combined with a list comprehension, you can gather all lowercase letters:

    使用str.islower()查找小写字符。结合列表理解,您可以收集所有小写字母:

    lowercase = [c for c in s if c.islower()]
    
  2. You could use a regular expression:

    您可以使用正则表达式:

    import re
    
    lc = re.compile('[a-z]+')
    lowercase = lc.findall(s)
    

The first method returns a list of individual characters, the second returns a list of character groups:

第一种方法返回单个字符列表,第二种方法返回字符组列表:

>>> import re
>>> lc = re.compile('[a-z]+')
>>> lc.findall('AbcDeif')
['bc', 'eif']

#4


4  

There are many methods to this, here are some of them:

有很多方法,这里有一些方法:

  1. Using the predefined function character.islower():

    使用预定义函数character.islower():

    >>> c = 'a'
    >>> print(c.islower()) #this prints True
    
  2. Using the ord() function to check whether the ASCII code of the letter is in the range of the ASCII codes of the lowercase characters:

    使用ord()函数检查字母的ASCII码是否在小写字符的ASCII码范围内:

    >>> c = 'a'
    >>> print(ord(c) in range(97,123)) #this will print True
    
  3. Checking if the letter is equal to it's lowercase:

    检查字母是否等于它的小写:

    >>> c = 'a'
    >>> print(c.lower()==c) #this will print True
    

But that may not be all, you can find your own ways if you don't like these ones: D.

但这可能不是全部,如果你不喜欢这些,你可以找到自己的方式:D。

Finally, let's start detecting:

最后,让我们开始检测:

d = str(input('enter a string : '))
lowers = [c for c in d if c.islower()]
#here i used islower() because it's the shortest and most-reliable one (being a predefined function), using this list comprehension is the most efficient way of doing this

#5


3  

You should use raw_input to take a string input. then use islower method of str object.

您应该使用raw_input来获取字符串输入。然后使用str对象的islower方法。

s = raw_input('Type a word')
l = []
for c in s.strip():
    if c.islower():
        print c
        l.append(c)
print 'Total number of lowercase letters: %d'%(len(l) + 1)

Just do -

做就是了 -

dir(s)

and you will find islower and other attributes of str

你会发现str的islower和其他属性

#6


1  

import re
s = raw_input('Type a word: ')
slower=''.join(re.findall(r'[a-z]',s))
supper=''.join(re.findall(r'[A-Z]',s))
print slower, supper

Prints:

Type a word: A Title of a Book
itleofaook ATB

Or you can use a list comprehension / generator expression:

或者您可以使用列表理解/生成器表达式:

slower=''.join(c for c in s if c.islower())
supper=''.join(c for c in s if c.isupper())
print slower, supper

Prints:

Type a word: A Title of a Book
itleofaook ATB

#1


29  

To check if a character is lower case, use the islower method of str. This simple imperative program prints all the lowercase letters in your string:

要检查字符是否为小写,请使用str的islower方法。这个简单的命令式程序打印字符串中的所有小写字母:

for c in s:
    if c.islower():
         print c

Note that in Python 3 you should use print(c) instead of print c.

请注意,在Python 3中,您应该使用print(c)而不是print c。


Possibly ending up with assigning those letters to a different variable.

可能最终将这些字母分配给不同的变量。

To do this I would suggest using a list comprehension, though you may not have covered this yet in your course:

要做到这一点,我建议使用列表理解,虽然你可能还没有在你的课程中涵盖这个:

>>> s = 'abCd'
>>> lowercase_letters = [c for c in s if c.islower()]
>>> print lowercase_letters
['a', 'b', 'd']

Or to get a string you can use ''.join with a generator:

或者要获得一个字符串,你可以使用'.inin和一个生成器:

>>> lowercase_letters = ''.join(c for c in s if c.islower())
>>> print lowercase_letters
'abd'

#2


10  

You can use built-in function any and generator.

您可以使用内置函数any和generator。

>>> any(c.islower() for c in 'Word')
True

>>> any(c.islower() for c in 'WORD')
False

#3


10  

There are 2 different ways you can look for lowercase characters:

您可以通过两种不同的方式查找小写字符:

  1. Use str.islower() to find lowercase characters. Combined with a list comprehension, you can gather all lowercase letters:

    使用str.islower()查找小写字符。结合列表理解,您可以收集所有小写字母:

    lowercase = [c for c in s if c.islower()]
    
  2. You could use a regular expression:

    您可以使用正则表达式:

    import re
    
    lc = re.compile('[a-z]+')
    lowercase = lc.findall(s)
    

The first method returns a list of individual characters, the second returns a list of character groups:

第一种方法返回单个字符列表,第二种方法返回字符组列表:

>>> import re
>>> lc = re.compile('[a-z]+')
>>> lc.findall('AbcDeif')
['bc', 'eif']

#4


4  

There are many methods to this, here are some of them:

有很多方法,这里有一些方法:

  1. Using the predefined function character.islower():

    使用预定义函数character.islower():

    >>> c = 'a'
    >>> print(c.islower()) #this prints True
    
  2. Using the ord() function to check whether the ASCII code of the letter is in the range of the ASCII codes of the lowercase characters:

    使用ord()函数检查字母的ASCII码是否在小写字符的ASCII码范围内:

    >>> c = 'a'
    >>> print(ord(c) in range(97,123)) #this will print True
    
  3. Checking if the letter is equal to it's lowercase:

    检查字母是否等于它的小写:

    >>> c = 'a'
    >>> print(c.lower()==c) #this will print True
    

But that may not be all, you can find your own ways if you don't like these ones: D.

但这可能不是全部,如果你不喜欢这些,你可以找到自己的方式:D。

Finally, let's start detecting:

最后,让我们开始检测:

d = str(input('enter a string : '))
lowers = [c for c in d if c.islower()]
#here i used islower() because it's the shortest and most-reliable one (being a predefined function), using this list comprehension is the most efficient way of doing this

#5


3  

You should use raw_input to take a string input. then use islower method of str object.

您应该使用raw_input来获取字符串输入。然后使用str对象的islower方法。

s = raw_input('Type a word')
l = []
for c in s.strip():
    if c.islower():
        print c
        l.append(c)
print 'Total number of lowercase letters: %d'%(len(l) + 1)

Just do -

做就是了 -

dir(s)

and you will find islower and other attributes of str

你会发现str的islower和其他属性

#6


1  

import re
s = raw_input('Type a word: ')
slower=''.join(re.findall(r'[a-z]',s))
supper=''.join(re.findall(r'[A-Z]',s))
print slower, supper

Prints:

Type a word: A Title of a Book
itleofaook ATB

Or you can use a list comprehension / generator expression:

或者您可以使用列表理解/生成器表达式:

slower=''.join(c for c in s if c.islower())
supper=''.join(c for c in s if c.isupper())
print slower, supper

Prints:

Type a word: A Title of a Book
itleofaook ATB