Table of contents
1.
Introduction
2.
What is Capitalize in Python?
3.
Python String capitalize() Method Syntax
4.
Parameters of Capitalize()
5.
Return Value
6.
Examples of Using the capitalize() Method in Python
6.1.
Example 1:Capitalizing the First Letter of a Sentence
6.2.
Python
6.3.
Example 2:Capitalizing the first letter of a mixed-case text
6.4.
Python
6.5.
Example 3:Using capitalize() when the first character is not an alphabet
6.6.
Python
7.
Ways to Capitalize the String
7.1.
slicing() and upper()
7.2.
Python
7.3.
title()
7.4.
Python
7.5.
capwords()
7.6.
Python
7.7.
regex
7.8.
Python
8.
Alternative to capitalize() in Python
8.1.
Python
9.
Advantages of Capitalize()
10.
Disadvantage of Capitalize()
10.1.
Python
11.
Frequently Asked Questions
11.1.
How do you input uppercase in Python?
11.2.
How do you capitalize in Python format?
11.3.
Do you capitalize Python coding?
11.4.
What is the Caps function in Python?
12.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Capitalize() in Python

Author Sagar Mishra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The capitalize() method in Python transforms the first character of a string to uppercase, while converting the rest of the characters to lowercase.

capitalize in python

Our today's topic for this blog is Capitalize() in Python. Here we will discuss all the features, along with examples. Let's start.

What is Capitalize in Python?

The Python String capitalize() method is used to capitalize the given string. It returns a string with the first letter capitalized, that is, in upper case, and the other characters of that string are converted to lower case letters.

Python String capitalize() Method Syntax

The syntax of Capitalize() in Python is as follows:

string.capitalize()


Here, 

  • The string is the given input. 
     
  • And the capitalize() method will change the input's first character into upper case and the rest into lower case.

Parameters of Capitalize()

In Python, the capitalize() method for strings does not take any parameters. It is a built-in method that operates directly on a string object

Return Value

The capitalize() method in Python always returns a string in which the first letter is in uppercase, and all other characters are in lowercase.

Examples of Using the capitalize() Method in Python

Example 1:Capitalizing the First Letter of a Sentence

  • Python

Python

sentence = "this is an example"
capitalized_sentence = sentence.capitalize()
print(capitalized_sentence)
You can also try this code with Online Python Compiler
Run Code

Output

This is an example

Example 2:Capitalizing the first letter of a mixed-case text

  • Python

Python

mixed_case_text = "thIs is An exAmpLe"
capitalized_text = mixed_case_text.capitalize()
print(capitalized_text)
You can also try this code with Online Python Compiler
Run Code

Output

This is an example

Example 3:Using capitalize() when the first character is not an alphabet

  • Python

Python

text = "123abc is an example"
capitalized_text = text.capitalize()
print(capitalized_text)
You can also try this code with Online Python Compiler
Run Code

Output

123abc is an example

Ways to Capitalize the String

Other than this, we can use many more methods to capitalize in python. Let's have a look at those methods too.

slicing() and upper()

In this approach, the first letter of the string was extracted using the slicing method. After that, we changed it to uppercase by using the string manipulation method upper(). This gives you access to the first letter of each word in the string, even those that are separated by spaces.

Sample Example:

  • Python

Python

string = "codingninjas"
print("Before capitalizing first letter:")
print(string)

result = string[0].upper() + string[1:]
print("After capitalizing first letter:")
print(result)
You can also try this code with Online Python Compiler
Run Code


Output:

output

title()

We can capitalize the first letter of each word and alter the rest letters to lowercase by using the string.title() method, which gives us the desired output. This method can also be used to format strings in HTML, JavaScript, and other programming languages.

Sample Example:

  • Python

Python

string = "codingninjas"
print("Before capitalizing first letter:")
print(string)

print("After capitalizing first letter:")
print(str.title(string))
You can also try this code with Online Python Compiler
Run Code


Output:

output

capwords()

A Python function called capwords() changes every word's first letter from lowercase to uppercase and vice versa. The function receives a string as a parameter value and outputs a string with the first letter capitalized.

Sample Example:

  • Python

Python

import string

myString = "codingninjas"
print("Before capitalizing first letter:")
print(myString)

print("After capitalizing first letter:")
result = string.capwords(myString)
print(result)
You can also try this code with Online Python Compiler
Run Code


Output:

output

You can compile it with online python compiler.

regex

Regex is known as a regular expression in Python. This special sequence of letters helps match or find the other strings. Regex allows you to capitalize each word by searching for the first character of the word. Using this method, Python's whitespace between words is left alone, and only the first character of each word is capitalized.

Sample Example:

  • Python

Python

import re

def convert_into_uppercase(match):
return match.group(0).upper()

string = "i love coding ninjas."
result = re.sub(r"\b\w", convert_into_uppercase, string)
print(result)
You can also try this code with Online Python Compiler
Run Code


Output:

output

Alternative to capitalize() in Python

We can use the join() method as an alternative to capitalize() in Python. While capitalize() capitalizes the first character of the string. We can also use the str.join() method to capitalize the first character of each word in a string. Let us discuss it with the help of an example.

  • Python

Python

str = "i love coding ninjas."
capitalized_words = [word.capitalize() for word in str.split()]
capitalized_string = ' '.join(capitalized_words)
print("String before performing title() method: ", str)
print("String after performing title() method: ",capitalized_string)
You can also try this code with Online Python Compiler
Run Code

Output:

output

Advantages of Capitalize()

  • The capitalize() method helps implement the task more easily.
     
  • It saves a lot of time in the case of operations and several tasks.
     
  • The capitalize() method help in data security.
     
  • The capitalize() method helps hide the execution details.

Disadvantage of Capitalize()

There is a disadvantage of Capitalize() in Python. That says you can not use any other special characters or digits as the first character of the input string.

Now, you must be curious about what will happen if we do not follow the rule above. Let's check this with the help of an example.

  • Python

Python

# Using special character
string1 = "$coding ninjas"

capitalized_string = string1.capitalize()

print("Using special character:", capitalized_string)

print("----------------------------------------")

# Using numeric character
string2 = "1coding ninjas"

capitalized_string = string2.capitalize()

print("Using numeric character:", capitalized_string)
You can also try this code with Online Python Compiler
Run Code


Output:

output

So, in the above example, we have seen that if the first character of the given string is either any special character or any digit, then the method capitalize() will not work. As a result, it will return the same output as the given input.

Check out this article - String slicing in Python

Frequently Asked Questions

How do you input uppercase in Python?

To input uppercase in Python, use the upper() method, e.g., my_string.upper().

How do you capitalize in Python format?

To capitalize in Python, apply the capitalize() method, e.g., my_string.capitalize().

Do you capitalize Python coding?

Capitalization in Python coding style follows conventions, like PEP 8, but it doesn't impact code execution.

What is the Caps function in Python?

We can use upper() or capitalize() for uppercase transformations.

Conclusion 

In this article, we have discussed capitalize() in Python. The capitalize() method in Python serves as a powerful tool for manipulating strings. It elegantly transforms the first character to uppercase, it helps in enhancing the text readability.

For learning more, check out some of our articles.

And many more on our platform Coding Ninjas Studio.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.

You can also consider our paid courses such as DSA in Python to give your career an edge over others!

Happy Learning!

Live masterclass