pythonstringcapitalize

Python How to capitalize nth letter of a string


I tried this: Capitalize a string. Can anybody provide a simple script/snippet for guideline?

Python documentation has capitalize() function which makes first letter capital. I want something like make_nth_letter_cap(str, n).


Solution

  • Capitalize n-th character and lowercase the rest as capitalize() does:

    def capitalize_nth(s, n):
        return s[:n].lower() + s[n:].capitalize()