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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12934997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:13:00  来源:igfitidea点击:

How to detect lowercase letters in Python?

pythonstringlowercaseletters

提问by JustaGuy313

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 入门课程,所以我的老师在我参加期中考试时不想看到复杂的解决方案。谢谢您的帮助!

回答by Mark Byers

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

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

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 ''.joinwith a generator:

或者要获取可以''.join与生成器一起使用的字符串:

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

回答by Mark Byers

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

回答by Martijn Pieters

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:

    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)
    
  1. 使用str.islower()查找小写字符。结合列表理解,您可以收集所有小写字母:

    lowercase = [c for c in s if c.islower()]
    
  2. 您可以使用正则表达式:

    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']

回答by Hussain

You should use raw_inputto take a string input. then use islowermethod of strobject.

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

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 islowerand other attributes of str

你会发现islower和其他属性str

回答by MrGeek

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

有很多方法可以做到这一点,这里是其中的一些:

  1. Using the predefined strmethod islower():

    >>> c = 'a'
    >>> c.islower()
    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:

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

    >>> c = 'a'
    >>> c.lower() == c
    True
    
  4. Checking if the letter is in the list ascii_lowercaseof the stringmodule:

    >>> from string import ascii_lowercase
    >>> c = 'a'
    >>> c in ascii_lowercase
    True
    
  1. 使用预定义的str方法islower()

    >>> c = 'a'
    >>> c.islower()
    True
    
  2. 使用该ord()函数检查字母的ASCII码是否在小写字符的ASCII码范围内:

    >>> c = 'a'
    >>> ord(c) in range(97, 123)
    True
    
  3. 检查字母是否等于它的小写形式:

    >>> c = 'a'
    >>> c.lower() == c
    True
    
  4. 检查字母是否在模块列表ascii_lowercasestring

    >>> from string import ascii_lowercase
    >>> c = 'a'
    >>> c in ascii_lowercase
    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 (probably) the most efficient way of doing this