Monday, August 12, 2019

codingdirectional: Change python string to lower or upper case

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.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...