GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — Python program to capitalize each word in a string
Python program to capitalize each word in a stringIn 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 stringThis 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: 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: 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 listThis 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: 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 all words in a string using the title() functionThis 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. « Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |