7

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
Ma0
  • 15,057
  • 4
  • 35
  • 65
coding girl
  • 183
  • 3
  • 15
  • Possible duplicate of [How to convert string to Title Case in Python?](https://stackoverflow.com/questions/8347048/how-to-convert-string-to-title-case-in-python) – user7440787 Oct 02 '19 at 07:38

2 Answers2

11

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"
Chris
  • 29,127
  • 3
  • 28
  • 51
  • 1
    nitpick: `join` [is faster](https://stackoverflow.com/questions/37782066/list-vs-generator-comprehension-speed-with-join-function) when provided with a list rather than a genexp. – Ma0 Sep 09 '19 at 08:54
  • Wow, never knew that. Thanks for the info :) I've editted – Chris Sep 09 '19 at 08:58
0

It can be done in another two methods:

Method#1

import string

def tojaden(jadencase):
    return string.capwords(jadencase)

print(tojaden("I'm iron man"))

Method#2

def jadencase(str):
    s1 = str.split()
    capWords = []
    for word in s1:
        caps = word.capitalize()
        capWords.append(caps)
    final_string = " ".join(capWords)
    print(final_string)


jadencase("it ain't me")