Skip to content

Python title function | Uppercase first character of the word

  • by

The title() method is used to convert the first character in every word of a given sentence in Python. Actually, this method returns a string with the first letter of each word capitalized.

string.title() 

Where string is the string you want to convert to the title case.

Python title function example

Simple example code capitalized the first character of each word of a given sentence (if the first character is a letter).

text = 'My favorite number is 7.'
print(text.title())

text = 'Hello robot 700'
print(text.title())

Output:

Python title function

Another example makes the first letter in each word upper case:

txt = "hello a23b12 and 7d7ea"

res = txt.title()

print(res)

Output: Hello A23B12 And 7D7Ea

Keep in mind that the title() function works based on whitespace, so it considers words separated by spaces. If you have non-alphabetic characters or special cases, you might need to handle those separately.

Comment if you have any doubts or suggestions on this Python function topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading