DEV Community

loading...

Discussion on: Daily Challenge #190 - capitalizeFirstLast

Collapse
amcmillan01 profile image
Andrei McMillan

python

import re


def to_uc(match_group):
    return match_group.group(0).upper()


def capitalize_first_last(in_str):
    in_str = in_str.lower().strip()
    in_str = re.sub(r'\s+', ' ', in_str)
    in_str = re.sub(r'(^[a-z])', to_uc, in_str)
    in_str = re.sub(r'([a-z]$)', to_uc, in_str)
    in_str = re.sub(r'(\s[a-z])', to_uc, in_str)
    in_str = re.sub(r'([a-z]\s)', to_uc, in_str)

    print in_str


# -----
capitalize_first_last('and still i    rise')
capitalize_first_last('when words fail music speaks')
capitalize_first_last('WHAT WE THINK WE BECOME')
capitalize_first_last('dIe wITh mEMORIEs nOt dREAMs')
capitalize_first_last('hello')

# -----
# AnD StilL I RisE
# WheN WordS FaiL MusiC SpeakS
# WhaT WE ThinK WE BecomE
# DiE WitH MemorieS NoT DreamS
# HellO