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:
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:
# ----codescracker.com---- 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:
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:
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:
text = text[:index] + ascVal + text[index+1:]
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.
This program uses list to do the same job as of previous program. The join() method joins or appends the thing.
# ----codescracker.com---- 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:
This program uses upper() function to capitalize all words of a given string by user at run-time.
# ----codescracker.com---- 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:
This is the shortest program to capitalize all words of a given string using title() function:
# ----codescracker.com---- 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.