Open In App

Python String Title method

Last Updated : 27 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python String title() method returns a new string after converting the first letter of every word to uppercase(capital) and keeping the rest of the letters lowercase.

Example: Make string title case

Python3




print("usE stRing title Method".title())


Output

Use String Title Method

String title() Method Syntax

string.title()

Parameters

  • title() doesn’t accept any parameter.

Return

string, converted to title case.

What is String title() Method?

String title() is an in-built function in Python that is used to change text in title format. It converts the first letter of every word to uppercase and other letters to lowercase and then returns this new string.

It is very useful function for string formatting. You can easily format texts for page titles or headings.

Note: Any non-alphabetic character is considered as the start of a new word in the title function, for example it will turn “do’s” to “Do’S”.

How to Use String title() Function

String title() function is a very easy string method. You just need to call the function with object string. Let’s look at how to convert a string to title with an example:

Python3




# using string title function
print("cOnverting to TiTle".title())


Output

Converting To Title

More Example on String title() Method

Here are some more examples on string title() method, for better understanding:

Example 1: Convert string to title

Python3




print("geeks_for_geeks".title())


Output:

Geeks_For_Geeks

Example 2: Basic usages of String title() Method

Python3




# conversion from mixed case
str1 = 'geeKs foR geEks'
print(str1, 'converted to using title():', str1.title())
 
# conversion from all lower case
str4 = 'geeks for geeks'.title()
print(str4, 'converted to using title():', str4.title())
 
# conversion from all UPPER CASE
str5 = 'WE ARE 1'.title()
print(str5, 'converted to using title():', str5.title())


Output: 

geeKs foR geEks converted to using title(): Geeks For Geeks
Geeks For Geeks converted to using title(): Geeks For Geeks
We Are 1 converted to using title(): We Are 1

Limitations of String title() Method

There are some limitations of string title() function. We have mentioned some of the limitations that you might notice:

Example 1: title() method considers any non-alphabet as a word boundary.

Python3




string = "He's smarter."
expected_string = "He's Smarter"
print("Expected:", expected_string, ", Actual:", string.title())


Output:

Expected: He's Smarter , Actual: He'S Smarter.

Explanation: The ‘s’ after He’ is converted to Capital letter, because after (apostrophe) string title() method considers the start of a new word, since s is the first letter after (apostrophe), thus it makes ‘s’ as capital letter.

Example 2: Different word boundaries other than space, when using title() Method

Here, even though there are no spaces separated words, still Python String title() method converts the String title case considering ‘-‘(hyphen) as word boundaries.

Python3




string = "geeks-for-geeks"
print(string, "converted using title():", string.title())


Output:

geeks-for-geeks converted using title(): Geeks-For-Geeks

Fixing the Limitations of title() Method

Now let’s look at the technique that you can use and avoid this unexpected behaviour of string title() function.

Using Regex to fix the unexpected behavior of String title() Method.

Python3




import re
 
 
def to_title(string):
    regex = re.compile("[a-z]+('[a-z]+)?", re.I)
    return regex.sub(lambda grp: grp.group(0)[0].upper() + grp.group(0)[1:].lower(),
                     string)
 
 
print(to_title("I won't be working tomorrow."))


Output:

I Won't Be Working Tomorrow.

In this article we have covered the definition, syntax and uses of string title() method in Python. We also covered the limitation of this function and how to work around it.

String title() function is very useful string method for text formatting.

Read More Python String Methods

Similar Reads:

FAQ on Python title() method

Q: What does the title() method do in Python?

The title() method is a built-in Python string method that returns a new string with the first character of each word capitalized, and all other characters in lowercase. It is used to convert a string into title case format.

Q: How does the title() method work?

The title() method scans the string and identifies word boundaries based on whitespace characters. It then converts the first character of each word to uppercase and the remaining characters to lowercase. It returns a new string with the modified formatting.

Q: What is the syntax for using the title() method?

The title() method is called on a string object using dot notation. The syntax is as follows: string.title()

Q: Does the title() method modify the original string?

No, the title() method does not modify the original string. Instead, it returns a new string with the modified formatting. If you want to store the modified string, you need to assign the result to a variable.



Previous Article
Next Article

Similar Reads

How to add Site Header, Site Title, Index Title in a Django Project?
Automatic admin interface one of the most powerful parts of Django. Metadata is read from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization’s internal management tool. It’s not intended for building your entire front end around. OVER
1 min read
Python String Methods | Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase & title)
Some of the string basics have been covered in the below articles Strings Part-1 Strings Part-2 The important string methods will be discussed in this article1. find("string", beg, end) :- This function is used to find the position of the substring within a string.It takes 3 arguments, substring , starting index( by default 0) and ending index( by
4 min read
Convert string to title case in Python
Title case is a style of writing that is used for the titles of articles, books, movies, and other works. The rules for writing in title case are: 1) Always capitalize the first word. 2) Capitalize all words except the following parts of speech: articles - a, an, the coordinating conjunctions - and, but, for, nor, or, so, yet short prepositions - t
2 min read
title driver method - Selenium Python
Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout – Navigating links using get method – Selenium Python. Just being able to go to places isn’t terribly useful. What
2 min read
numpy string operations | title() function
numpy.core.defchararray.title(arr): function is used to Return element-wise title cased version of string or unicode.Title case words start with uppercase characters, all remaining cased characters are lowercase. Parameters: arr : [ array_like ] Input array which may be str or unicode. Returns : [ndarray] Output array of str or unicode, depending o
1 min read
Python | Pandas Series.str.lower(), upper() and title()
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, making importing and analyzing data much easier. Python has some inbuilt methods to convert a string into a lower, upper, or Camel case. But these methods don't work on lists and other multi-st
4 min read
How to get title of a webpage using Selenium in Python?
The title method is used to retrieve the title of the webpage the user is currently working on. It gives the title of the current webpage loaded by the driver in selenium, if webpage has no title a null string will be returned. Webpage title :A page title, also known as a title tag, is a short description of a webpage and appears at the top of a br
2 min read
Python - Obtain title, views and likes of YouTube video using BeautifulSoup
In this article, we will learn how can we obtain data (like title, views, likes, dislikes etc) from any YouTube video using a Python script. For this task, we are going to use very famous library for web scraping BeautifulSoup and Requests. Modules required and Installation : Requests : Requests allows you to send HTTP/1.1 requests extremely easily
2 min read
Python IMDbPY – Getting title from searched movie
In this article we will see how we can get the title of movie from the searched list of movies. We use search_movie method to find all the related movies. search_movie method returns list and each element of list work as a dictionary i.e. they can be queried by giving the key of the data, here key will be title. Syntax : movies[0][‘title’]Here movi
2 min read
turtle.title() function in Python
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.title() This function is used to set the title of turtle-window. It requires only one argument as "titlestring" a string, to a
1 min read