Python String title()

Python String title() method

Python String title() method returns the title representation of the given string. A title is a string in which every first alphabet in the words of the string is an upper case, and rest of the characters are lower case.

Consider that given string is in x.

x = "hello world"

Then the return value of x.title() is

Hello World

In this tutorial, you will learn the syntax and usage of String title() method in Python language.

Syntax of title() method

The syntax of String title() method in Python is given below.

str.title()

Parameters

The string title() method takes no parameters.

Return value

The string title() method returns a String value.

Examples

1. Converting given string to a title string in Python

In this example, we take a string value with all lower case characters in variable x. We have to convert this string to a title.

Steps

  1. Given a string in x.
  2. Call title() method on the string x.
x.title()
  1. title() method returns the title representation of string x. Assign the returned value to a variable, say x_title.
x_title = x.title()
  1. Print the title string to output.
print(x_title)

Program

The complete program to convert a given string to a title string is given below.

Python Program

# Take a string
x = "hello world"

# Convert to title string
x_title = x.title()

# Print title string
print(x_title)
Run Code Copy

Output

Hello World

The first alphabetical character in each word is converted to a string, and the rest of the characters to lower case.

2. Converting string with all upper case characters to a title string in Python

In this example, we take a string value with all upper case characters in variable x. We have to convert this string to a title.

Follow the same steps that we have mentioned in the previous example.

Python Program

# Take a string
x = "HELLO WORLD"

# Convert to title string
x_title = x.title()

# Print title string
print(x_title)
Run Code Copy

Output

Hello World

3. Converting string containing words starting with numbers to a title string in Python

In this example, we take a string value with all upper case characters in variable x. We have to convert this string to a title.

Follow the same steps that we have mentioned in the previous example.

Python Program

# Take a string
x = "hello 123world"

# Convert to title string
x_title = x.title()

# Print title string
print(x_title)
Run Code Copy

Output

Hello 123World

Even though there are numeric digits at the starting of the word ‘123world’, the first alphabet ‘w’ has been transformed to upper case.

The same behavior hold for the title() method, if the words in given string start with any other non-alphabetical characters.

Summary

In this tutorial of Python String Methods, we learned about String title() method, its syntax, and examples.

Related Tutorials

Code copied to clipboard successfully 👍