0

How to capitalize specific letters in string ?

I want to write a function capitalizes first and fourth letters in a name like "macdonald". I tried string.capitalize() but just first letter done and can't take arguments . Thanks for help

10th Apr 2022, 1:30 PM
kareem nada
kareem nada - avatar
3 Answers
+ 2
You can do it manually, If you know it's always first and 4th then use replace method.. title() makes 'Mac Donald' if you have a space before D. Every character After a space is capitalized.. hope it helps..
10th Apr 2022, 2:24 PM
Jayakrishna šŸ‡®šŸ‡³
+ 2
kareem nada [edited] x='macdonald' y=x[0].capitalize() z=x[3].capitalize() print(str(y)+str(x[1:3]+str(z)+str(x[4:9])))
10th Apr 2022, 2:26 PM
JOKER
JOKER - avatar
+ 1
Make a `list` from the string, replace items at index 0, and 3 by their respective uppercase letter (if they were alphabets), then use str.join() method to glue `list` elements into a new string.
10th Apr 2022, 10:15 PM
Ipang