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)
.
Capitalize n-th character and lowercase the rest as capitalize()
does:
def capitalize_nth(s, n):
return s[:n].lower() + s[n:].capitalize()
my_string[:n] + my_string[n].upper() + my_string[n + 1:]
Or a more efficient version that isn't a Schlemiel the Painter's algorithm:
''.join([my_string[:n], my_string[n].upper(), my_string[n + 1:]])
x = "string"
y = x[:3] + x[3].swapcase() + x[4:]
Output
strIng
Code
Keep in mind that swapcase
will invert the case whether it is lower or upper.
I used this just to show an alternate way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With