Python Program to Capitalize each Word in a String

In this article, we've created some programs in Python, to capitalize each words in a string entered by user. Here are the list of programs:

Capitalize First Letter of Each Word of Given String

This program capitalizes first letter of each word of a string entered by user. The question is, write a Python program to capitalize each word of given string. Here is its answer:

print("Enter the String: ")
text = str(input())
textLen = len(text)
for i in range(textLen):
    ch = text[i]
    if i==0:
        ascVal = ord(ch)
        if ascVal>=97 and ascVal<=122:
            ascVal = ascVal-32
            ascVal = chr(ascVal)
            index = 0
            text = text[:index] + ascVal + text[index+1:]
    if ch==" ":
        ch = text[i+1]
        ascVal = ord(ch)
        if ascVal>=97 and ascVal<=122:
            ascVal = ascVal-32
            ascVal = chr(ascVal)
            index = i+1
            text = text[:index] + ascVal + text[index+1:]
print("\nThe New String is:")
print(text)

Here is its sample run:

capitalize each word in string python

Now supply the string say welcome to python and press ENTER key to capitalize all the three words of given string as shown in the snapshot given below:

capitalize all words in python

Note - The ord() function is used to convert a character to an integer (ASCII Value). Because it returns the Unicode of a character passed as its argument.

Note - The chr() is used to return equivalent character of Unicode value passed as its argument.

The dry run of above program with user input welcome to python goes like:

In above program, there are two if inside the loop. The first condition is applied to check and capitalize the first character of string, or first character of first word. And the second if is applied to check for space and then operate with next character from this space, to check and capitalize.

Capitalize First Letter of Each Word using List

This program uses list to do the same job as of previous program. The join() method joins or appends the thing.

print("Enter the String: ")
text = str(input())
textLen = len(text)
textList = list(text)
for i in range(textLen):
    ch = textList[i]
    if i==0:
        ascVal = ord(ch)
        if ascVal>=97 and ascVal<=122:
            ascVal = ascVal-32
            ascVal = chr(ascVal)
            index = 0
            textList[index] = ascVal
            text = "".join(textList)
    if ch==" ":
        ch = textList[i+1]
        ascVal = ord(ch)
        if ascVal>=97 and ascVal<=122:
            ascVal = ascVal-32
            ascVal = chr(ascVal)
            index = i+1
            textList[index] = ascVal
            text = "".join(textList)
print("\nThe New String is:")
print(text)

Here is its sample run with user input, hello Hello 23hello 53Hello hELLO /.- hello:

python capitalize all words in string

Capitalize All Words of String using List and upper()

This program uses upper() function to capitalize all words of a given string by user at run-time.

print(end="Enter the String: ")
text = str(input())
textLen = len(text)
textList = list(text)
for i in range(textLen):
    ch = textList[i]
    if i==0:
        ascVal = ord(ch)
        if ascVal>=97 and ascVal<=122:
            index = 0
            textList[index] = ch.upper()
            text = "".join(textList)
    if ch==" ":
        ch = textList[i+1]
        ascVal = ord(ch)
        if ascVal>=97 and ascVal<=122:
            index = i+1
            textList[index] = ch.upper()
            text = "".join(textList)
print("\nThe New String is: " + text)

Here is its sample run with user input, welcome to fresherearth:

capitalize words in string python

Capitalize all Words in String using title() Function

This is the shortest program to capitalize all words of a given string using title() function:

print(end="Enter the String: ")
text = str(input())
text = text.title()
print("\nThe New String is: " + text)

This program produces the same output as of previous program.

Python Online Test


« Previous Program Next Program »