Python How to capitalize nth letter of a string

Trojosh picture Trojosh · Apr 7, 2013 · Viewed 35k times · Source

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).

Answer

jfs picture jfs · Apr 7, 2013

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

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