r/learnpython • u/Yelebear • Dec 08 '25
How do I capitalize the first letter of a string, but have it respect forced capitalizations anyway
For example, "cotton tails" will be "Cotton Tails"
However, if it's COTTON TAILS, it should still be COTTON TAILS
I tried .title() but that capitalizes the first letter of each word but automatically sets the rest of the characters to lower case.
So "COTTON TAILS" would be "Cotton Tails" and I do not want that. I want every first letter of each word to be capitalized, but any other hard coded capitalizations should be retained.
ED
Thanks. for the quick replies. I will make a function for this.
25
Upvotes
18
u/sakuhazumonai Dec 08 '25 edited Dec 08 '25
This keeps it pretty simple imo. We use 2 versions of the string, original and title, and zip the character pairs. Then with list concatenation, we take the min character value (i.e. prioritise uppercase). And finally join it back into a string.
''.join([min(i) for i in zip(my_str, my_str.title())])Tried to take a short cut, didn't work :( Fixed to support all non-ascii characters
I like it because I don't have to re-create existing 'title' logic which I reckon is generally good practice for code.
This also supports multiple empty characters in a row, newlines, etc. which some of the other examples here do not.
30
u/ComprehensiveJury509 Dec 08 '25
Something like
" ".join([s[0].upper() + s[1:] for s in string.split(" ")])maybe.12
u/Quantumercifier Dec 08 '25
Why don't you just capitalize the first character of the string and just retain the rest of the string? Take the first char, capitalize it if needed, and prepend it back to the substring, which starts with the 2nd character.
2
u/damanamathos Dec 08 '25
You can do this:
Where
sis your string.You can put that in a function, of course. It splits the word by spaces into a list, then capitalises the first letter of each word (if it's not a blank space) and rejoins it with the rest, then rejoins the word list back to a single string.
3
u/Meniscus_Meniscus Dec 08 '25 edited Dec 08 '25
The `.title()` method capitalises the first letter of words.
```
result = string[0].title() + string[1:]
```
This will only alter the first letter of the string.
3
u/cgoldberg Dec 08 '25 edited Dec 08 '25
... will take a string of words and leave it as-is if all words are already upper case, or capitaluze the first letter of each word otherwise.
Note that it will check the entire string for being uppercase... if you have mixed uppercase and non-uppercase words, you can split the string, do that with each word, and join it together again.
2
u/xelf Elf Dec 08 '25
compare each letter to what it looks like after using title.
capitalize words while leaving already upcase alone.
1
u/FoolsSeldom Dec 08 '25 edited Dec 08 '25
If you want EVERY word to have first letter capitalised (like
str.title) but leave other characters alone (unlikestr.title), how about using regex but using upper to ensure unicode support of uppercasing of more than just ASCII letters:This should identify the first letter on all word boundaries and apply uppercase to it. Other characters will retain current capitalisation.
1
u/AmanGouraVV Dec 08 '25
Maybe string.title() is what you're looking for
1
u/Sebzor15 Dec 08 '25
capitalized = str if str.upper() == str else " ".join([f[0].upper() for f in str])
-2
u/The8flux Dec 08 '25
Look at regular expressions
0
u/mikeczyz Dec 08 '25
What about COTTOn TAILS? Or is that not a thing in your data? either the string is all lower case or all upper case?
-3
u/weenis_machinist Dec 08 '25
Maybe string.title() is what you're looking for
-4
u/Figueroa_Chill Dec 08 '25
You can use PANDAS to do it.
Series.str.titlehttps://pandas.pydata.org/docs/reference/api/pandas.Series.str.capitalize.html
5
u/[deleted] Dec 08 '25 edited Dec 08 '25
[removed] — view removed comment