How to capitalize the first letter in python?

Sometimes, we want the initial character of a string to be in uppercase, or the initial character of every word in a string to be in uppercase. This article discusses different approaches to capitalize the first letter of a string in python.

Capitalizing the first character of the string using capitalize() function

The capitalize() function returns the copy of the original string by converting the initial character of the string to the uppercase and all the remaining characters to the lowercase. The syntax for capitalize() function is given below.

#syntax:
string.capitalize()

Example,

string_1 = "pythoN Programming"
print("string_1: ", string_1)
string_2 = string_1.capitalize()
print("string_2: ", string_2)

The above code returns the output as

string_1:  pythoN Programming
string_2:  Python programming
Capitalizing the first character of the string using slicing

The syntax for slicing is [start: stop]. The value for the start and stop field is optional and if the value is not provided it sets the default value for start to '0' and stop to string length.

In the example given below, we converted the initial character of the string string_1 = 'pythoN Programming' to uppercase using the upper() function and concatenated the remaining character as it is.

string_1 = pythoN Programming"
print("string_1: ", string_1)
string_2 = string_1[0].upper() + string_1[1:]
print("string_2: ", string_2)

The above code gives the output as

string_1:  pythoN Programming
string_2:  PythoN Programming

Now, If we want to convert the first character to uppercase and all the remaining characters to lowercase we use both the upper() and lower() functions respectively.

string_1 = "pythoN Programming"
print("string_1: ", string_1)
string_2 = string_1[0].upper() + string_1[1:].lower()
print("string_2: ", string_2)

Output

string_1:  pythoN Programming
string_2:  Python programming
Capitalizing the first
character of the string using for loop

Consider a string string_1 = 'pythoN Programming'. In the example given below, we convert the first character of string_1 to uppercase and all the remaining character to lowercase and store the resulting string in the variable string_2.

The string_2 is initialize with the first character of string_1 converted to uppercase letter using the upper() function. A for loop iterates over string_1 from the range of 1 to length of the string_1. In each iteration the character of string_1 is converted to lowercase using lower() function and is concatenated to variable string_2.

string_1 = "pythoN Programming"
print("string_1: ", string_1)
string_2 = string_1[0].upper()
for i in range(1, len(string_1)):
    string_2 += string_1[i].lower()
print("string_2: ", string_2)

The above code gives output as

string_1:  pythoN Programming
string_2:  Python programming

Capitalizing the first character of the each word in the string

Capitalizing the first
character of each word in the string using str.title() function

In python, str.title() is a inbuilt function that converts the initial character of each word in the string to the uppercase and all the remaining characters of the string to the lowercase. The syntax for the str.title() function is given below.

#syntax:
str.title(string)

Example,

string_1 = "pythoN programming"
print("string_1: ", string_1)
string_2 = str.title(string_1)
print("string_2: ", string_2)

The above program returns the output as

string_1:  pythoN programming
string_2:  Python Programming
Capitalizing the first
character of each word in the string using capwords() function

The capwords() function can be imported from the string module. The capwords() function takes the string as a parameter and converts the initial character of each word in the string to the uppercase and all the remaining characters of the string to the lowercase.

The syntax for capwords() is given below.

#syntax:
from string import capwords
capwords.(string)

Example,

from string import capwords
string_1 = "pythoN programming"
print("string_1: ", string_1)
string_2 = capwords(string_1)
print("string_2: ", string_2)

Output

string_1:  pythoN programming
string_2:  Python Programming
Capitalizing the first
character of each word in the string using split() + capitalize() + join()

Consider a string string_1 = "pythoN programming". The string_1.split() returns a list of string and stores in list_string = ['pythoN', 'programming']. The capitalize() function capitalizes each word in the list using the list comprehension and stores the list in variable cap_words as ['Python', 'Programming'].

The Join() method returns a string, which is the concatenation of string elements in the iterable cap_words and stores the resulted string in string_2 = Python Programming.

string_1 = "pythoN programming"
print("string_1: ", string_1)
list_string = string_1.split()
print("list_string: ", list_string)
cap_words = [word.capitalize() for word in list_string]
print("cap_words: ", cap_words)
string_2 = ' '.join(cap_words)
print("string_2: ", string_2)

The above code returns the output as

string_1:  pythoN programming
list_string:  ['pythoN', 'programming']
cap_words:  ['Python', 'Programming']
string_2:  Python Programming
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions