Python String | islower () method

islower | Python Methods and Functions | String Variables

Python has many helper functions in its libraries that always help us with some day to day tasks. Let's see how the string islower () method works which actually checks if all characters in the string are lowercase.

Syntax: string .islower ()

Parameters: None

Returns: True if all the letters in the string are in lower case and False if even one of them is in upper case.

Code # 1: Demonstration of islower ()

# Python3 demo code
# islower () working

 
# initialization string

islow_str = " pythonengineering "

not_islow = "Geeksforgeeks"

 
# check which line
# completely below

print ( "Is pythonengineering full lower? : " + str (islow_str.islower ()))

print ( "Is Geeksforgeeks full lower?: " + str (not_islow.islower ()))

Exit:

 Is pythonengineering full lower?: True Is Geeksforgeeks full lower?: False 

Practical use: This function can be used in many ways and has many practical uses. such applications are lowercase validation, proper name validation, sentence validation, which requires all lowercase letters. Below is a small example demonstrating the use of the islower () method.

Code # 2: Practical Demonstration islower () method

# Python3 demo code
# using the islower () method

 
# check proper names.
# capitalized nouns

 

test_str = " Geeksforgeeks is most rated Computer

Science portal and is highly recommended "

  
# line splitting

list_str = test_str.split ()

 

count = 0

 
# counting lowercase

for i in list_str:

if (i.islower ()):

  count = count + 1

 
# print proper nouns

print ( "Number of proper nouns in this sentence is:"

+ str ( len (list_str) - count))

Output:

 Number of proper nouns in this sentence is: 3 




Python String | islower () method: StackOverflow Questions

Answer #1

itertools.groupby is a tool for grouping items.

From the docs, we glean further what it might do:

# [k for k, g in groupby("AAAABBBCCDAABBB")] --> A B C D A B

# [list(g) for k, g in groupby("AAAABBBCCD")] --> AAAA BBB CC D

groupby objects yield key-group pairs where the group is a generator.

Features

  • A. Group consecutive items together
  • B. Group all occurrences of an item, given a sorted iterable
  • C. Specify how to group items with a key function *

Comparisons

# Define a printer for comparing outputs
>>> def print_groupby(iterable, keyfunc=None):
...    for k, g in it.groupby(iterable, keyfunc):
...        print("key: "{}"--> group: {}".format(k, list(g)))
# Feature A: group consecutive occurrences
>>> print_groupby("BCAACACAADBBB")
key: "B"--> group: ["B"]
key: "C"--> group: ["C"]
key: "A"--> group: ["A", "A"]
key: "C"--> group: ["C"]
key: "A"--> group: ["A"]
key: "C"--> group: ["C"]
key: "A"--> group: ["A", "A"]
key: "D"--> group: ["D"]
key: "B"--> group: ["B", "B", "B"]

# Feature B: group all occurrences
>>> print_groupby(sorted("BCAACACAADBBB"))
key: "A"--> group: ["A", "A", "A", "A", "A"]
key: "B"--> group: ["B", "B", "B", "B"]
key: "C"--> group: ["C", "C", "C"]
key: "D"--> group: ["D"]

# Feature C: group by a key function
>>> # islower = lambda s: s.islower()                      # equivalent
>>> def islower(s):
...     """Return True if a string is lowercase, else False."""   
...     return s.islower()
>>> print_groupby(sorted("bCAaCacAADBbB"), keyfunc=islower)
key: "False"--> group: ["A", "A", "A", "B", "B", "C", "C", "D"]
key: "True"--> group: ["a", "a", "b", "b", "c"]

Uses

Note: Several of the latter examples derive from Víctor Terrón"s PyCon (talk) (Spanish), "Kung Fu at Dawn with Itertools". See also the groupby source code written in C.

* A function where all items are passed through and compared, influencing the result. Other objects with key functions include sorted(), max() and min().


Response

# OP: Yes, you can use `groupby`, e.g. 
[do_something(list(g)) for _, g in groupby(lxml_elements, criteria_func)]

Tutorials