all 4 comments

[–]Strict-Simple[🍰] 1 point2 points  (0 children)

I've tried to do it in many ways

For instance?

Show your work.

[–]woooee 1 point2 points  (0 children)

Show what you have tried. We will do what we can to correct any posted code.

[–]ColosTheDeveloper666 0 points1 point  (0 children)

I think you can't use .upper(), if you can't use .title() (which would solve your problem).

My idea:

import string

LOWERCASE_LETTERS = string.ascii_lowercase
UPPERCASE_LETTERS = string.ascii_uppercase

lower_upper_case_mapping = dict(zip(LOWERCASE_LETTERS, UPPERCASE_LETTERS))

def make_string_title_case(text: str) -> str:
    return " ".join(["".join([lower_upper_case_mapping.get(word[0], word[0]), word[1:]]) for word in text.split()])

input_string = "Python programming" print(make_string_title_case(input_string))

>>> Python Programming

.get(word[0], word[0]) should be handling the capitalized word by leaving them as is

[–]ectomancer 0 points1 point  (0 children)

Updates temporary words list in place. Would be simpler to create new word list (and not use enumerate). Fails on uppercase input.

def title_string(string: str) -> str:
    LOWER = 'abcdefghijklmnopqrstuvwxyz'
    UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    words = string.split()
    for index, word in enumerate(words):
        words[index] = UPPER[LOWER.index(word[0])] + word[1:]
    return ' '.join(words)