Python capitalize the first letter without changing the rest | Example code

Just capitalize the first letter with str.upper() and concatenate the rest unchanged to capitalize the first letter without changing the rest in python.

string[0].upper() + string[1:]

Python capitalize first letter only Example

Python simple example code.

str1 = 'hello world'
str1 = str1[0].upper() + str1[1:]

print(str1)

Output:

Python capitalize the first letter without changing the rest

If the first character is an integer

str1 = '12hello world'

for i, c in enumerate(str1):
    if not c.isdigit():
        break

str2 = str1[:i] + str1[i:].capitalize()
print(str2)

Output:

12Hello world

Do comment if you have any doubts and suggestions on this Python capitalize topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.