Python program to capitalize each word in a string

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

Capitalize the first letter of each word of the given string

This program capitalizes the first letter of each word of a string entered by the user. The question is: write a Python program to capitalize each word of a 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 the ENTER key to capitalize all three words of the 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() function is used to return the equivalent character of the Unicode value passed as its argument.

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

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

Capitalize the first letter of each word using a list

This program uses lists to do the same job as the 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 a sample run with user input: hello Hello 23hello 53Hello hELLO /.- hello:

python capitalize all words in string

Capitalize all words in a string using List and upper()

This program uses the upper() function to capitalize all words of a given string at runtime.

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 codescracker:

capitalize words in string python

Capitalize all words in a string using the title() function

This is the shortest program to capitalize all words of a given string using the 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 the previous program.

Python Online Test


« Previous Program Next Program »


Liked this post? Share it!