Get all lowercase letters python

The lower() method converts all uppercase characters in a string into lowercase characters and returns it.

Example

message = 'PYTHON IS FUN'

# convert message to lowercase print(message.lower())

# Output: python is fun


Syntax of String lower()

The syntax of lower() method is:

string.lower()

lower() Parameters()

lower() method doesn't take any parameters.


lower() Return value

lower() method returns the lowercase string from the given string. It converts all uppercase characters to lowercase.

If no uppercase characters exist, it returns the original string.


Example 1: Convert a string to lowercase

# example string
string = "THIS SHOULD BE LOWERCASE!"

print(string.lower())

# string with numbers # all alphabets should be lowercase string = "Th!s Sh0uLd B3 L0w3rCas3!"

print(string.lower())

Output

this should be lowercase!
th!s sh0uld b3 l0w3rcas3!

Example 2: How lower() is used in a program?

# first string
firstString = "PYTHON IS AWESOME!"

# second string
secondString = "PyThOn Is AwEsOmE!"

if(firstString.lower() == secondString.lower()):

print("The strings are same.") else: print("The strings are not same.")

Output

The strings are same.

Note: If you want to convert to uppercase string, use upper(). You can also use swapcase() to swap between lowercase to uppercase.

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.

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.

Get all lowercase letters python

asked Oct 17, 2012 at 13:04

4

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:

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

Note that in Python 3 you should use print(c) instead of 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:

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

answered Oct 17, 2012 at 13:07

Mark ByersMark Byers

779k183 gold badges1551 silver badges1440 bronze badges

0

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)
    

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

answered Oct 17, 2012 at 13:10

Martijn PietersMartijn Pieters

984k273 gold badges3872 silver badges3234 bronze badges

6

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

  1. Using the predefined str method 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_lowercase of the string module:

    >>> 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.

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

answered May 4, 2016 at 20:32

Get all lowercase letters python

DjaouadNMDjaouadNM

21.5k4 gold badges29 silver badges52 bronze badges

2

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

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

answered Oct 17, 2012 at 13:13

Get all lowercase letters python

HussainHussain

4,8095 gold badges44 silver badges68 bronze badges

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

answered Oct 17, 2012 at 13:10

If you don't want to use the libraries and want simple answer then the code is given below:

  def swap_alpha(test_string):
      new_string = ""
      for i in test_string:
          if i.upper() in test_string:
              new_string += i.lower()

          elif i.lower():
                new_string += i.upper()

          else:
              return "invalid "

      return new_string


user_string = input("enter the string:")
updated = swap_alpha(user_string)
print(updated)

answered Jun 14, 2020 at 12:47

Get all lowercase letters python

How do you get lowercase letters in Python?

Python String islower() The islower() method returns True if all alphabets in a string are lowercase alphabets. If the string contains at least one uppercase alphabet, it returns False.

How do I print only lowercase in Python?

islower() is a more succinct way of checking the case of the letter. Similarly, . isupper() would print only the capital letters.

How do you check if a string is all lowercase Python?

Call str. islower() with str as a string to determine if str is all lowercase. Call str. isupper() to determine if str is all uppercase.

How do I get a list of all letters in Python?

Use string..
alphabet_string = string. ascii_lowercase. Create a string of all lowercase letters..
alphabet_list = list(alphabet_string) Create a list of all lowercase letters..
print(alphabet_list).