Python String title() Method

29-12-22 Ahmed Obaid 1051 0

​The title() function converts the first letter of each word to uppercase and the remaining letters to lowercase. and returns a new string.


How to formulate it like this:

string.title()

Argument: The title() function takes no argument.

return value: The title() function returns a new string with the first letter of each word converted to uppercase


Example:

string = "learn  python  with ahmed obaid"
print(string.title())

The output will be:

Learn  Python  With Ahmed Obaid

If a string contains all uppercase letters, a new string is returned with the first letter of each word converted to uppercase and the remaining letters to lowercase.

Example:

string = "LEARN  PYTHON  WITH AHMED OBAID"
print(string.title())

The output will be:

Learn  Python  With Ahmed Obaid

If the string is made up of non-alphabetic characters, symbols, or numbers, the title() function will return the original string as is.

Example:

string = "9656731!@#$%^>&"
print(string.title())

The output will be:

9656731!@#$%^>&

If the string has spaces between words, a new string will be returned with the first letter of each word converted to uppercase and letters Remaining to lowercase and keep the dashes the same.

 Example:

string = "learn-python-with-ahmed-obaid"
print(string.title())

 The output will be:

Learn-Python-With-Ahmed-Obaid

 If the string was originally formatted as a title. The title() function will return the original string as is.

 Example:

string = "Learn  Python  With Ahmed Obaid"
print(string.title())

 The output will be:

Learn  Python  With Ahmed Obaid

 The title() function takes any non-alphabetic characters (symbols) as a word terminator

 Example:

string = "It's Python"
print(string.title())

 The output will be:

It'S Python

 External sources:

 Built-in functions - official Python documentation

 If you have any questions or concerns, leave them in the comments



Tags


Python Python String string methods python python string title method

Share page