Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 23130

codingdirectional: Change python string to lower or upper case

$
0
0

In this article, we will create a function which will take in a string and then change the word within that string to either all uppercases if most of the words within that string are uppercase or all lowercases if most of those words are either lowercase or the word counts for the uppercase word and lowercase word are equal.

def solve(s):
    list_string = list(s)
    upper = 0
    lower = 0
    for word in list_string:
        if word.isupper():
            upper += 1
        else:
            lower += 1
    if upper > lower :
        return s.upper()
    elif upper < lower:
        return s.lower()
    else:
        return s.lower()

Very simple solution, if you have better idea leave your comment below.


Viewing all articles
Browse latest Browse all 23130

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>