Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jaden Casing String: How to return a sentence string with capitalised words?

I want to create a function that takes a short text and returns it in the so-called Jaden Case mode where each word's first letter is a capital (best reference I could find).

For example "Hi, I'm twenty years old" should return "Hi I'm Twenty Years Old".

I have tried to solve this problem on my own, but the letter after the apostrophe sign becomes a capital whereas it shouldn't..

My attempt:

def toJadenCase(string):
    for letter in string:
        if letter == "\'" :
            letter[+1].lowercase()
        else:
            return string.title()
    return string
like image 229
coding girl Avatar asked Sep 09 '19 08:09

coding girl


1 Answers

Use str.split and str.capitalize:

s = "Hi, I'm twenty years old"
' '.join([i.capitalize() for i in s.split()])

Output:

"Hi, I'm Twenty Years Old"
like image 157
Chris Avatar answered Nov 15 '22 16:11

Chris