I'm trying to make a function that takes in a string as an input. It then moves the capital letters in the string to the front, and capitalizes lower-case letters. Lower-case letters and spaces should be kept in the same position relative to their location in the original string.
For example:
unravel_message(' neHAw moPPnthY')
should return 'HAPPY NEW MONTH' as the output.
and unravel_message('ALL CAPITALS')
should return 'ALLCAPITALS ' as the output.
(the space remains in the same location; only the capitals are moved back)
My Attempt:
def unravel_message(word):
return ''.join([i for i in word if i == i.upper()]) + ''.join([i.upper() for i in word if i == i.lower()])
for my function, unravel_message(' neHAw moPPnthY') outputs ' HA PPY NEW MONTH'. Which isn't quite right. I think this may be because of the ''.join(). I would like to revise this function to use a single list comprehension wihtout imports if possible. If the inputs are all lowercase, I would also like to include an assertion statement addressing an error but I'm not sure how to do this.