- Python Basic Programs
- Python Program Examples
- Python Print Hello World
- Python Get Input from User
- Python Add Two Numbers
- Add Subtract Multiply Divide
- Python Check Even or Odd
- Python Check Prime or Not
- Python Check Alphabet or Not
- Python Check Vowel or Not
- Python Check Leap Year or Not
- Check Reverse equal Original
- Check Positive Negative Zero
- Python Check Armstrong or Not
- Python Check Palindrome or Not
- Python Check Perfect Number
- Python Find Reverse of Number
- Python Count Digits in Number
- Python Add Digits of Number
- Sum of First and Last Digits
- Python Product of Mid Digits
- Sum of Squares of Digits
- Interchange Digits of Number
- Python Sum of n Numbers
- Python Print ASCII Values
- Python Swap Two Numbers
- Python Swap Two Variables
- Python Fahrenheit to Celsius
- Python Celsius to Fahrenheit
- Python Display Calendar
- Python Days into Years, Weeks
- Find Largest of Two Number
- Find Largest of Three Number
- Python Print Fibonacci Series
- Generate Armstrong Numbers
- Python Make Simple Calculator
- Python Add Binary Numbers
- Binary Number Multiplication
- Python Mathematical Programs
- Find Sum of Natural Numbers
- Find Average of n Numbers
- Python Print Multiplication Table
- Print Table using Recursion
- Python Find Average Percentage
- Python Find Grade of Student
- Find Square Root of Number
- Python Print Prime Numbers
- Find Numbers Divisible by
- Python Find Factors of Number
- Python Find Factorial of a Number
- Python Find HCF & LCM
- Python Kilometres to Miles
- Python Find Area of Square
- Python Find Area of Rectangle
- Python Find Area of Triangle
- Python Find Area of Circle
- Python Find Perimeter of Square
- Find Perimeter of Rectangle
- Python Find Perimeter of Triangle
- Find Circumference of Circle
- Python Simple Interest
- Python Solve Quadratic Equation
- Python Different Set of Operations
- Python Display Powers of 2
- Python Find nCr & nPr
- Python Pattern Programs
- Python Print Pattern Programs
- Python Print Diamond Pattern
- Python Print Floyd's Triangle
- Python Print Pascal's Triangle
- Python List Programs
- Python Count Even/Odd in List
- Python Positive/Negative in List
- Python Even Numbers in List
- Python Odd Numbers in List
- Python Sum of Elements in List
- Sum of Odd/Even Numbers
- Python Element at Even Position
- Python Element at Odd Position
- Python Search Element in List
- Python Largest Number in List
- Python Smallest Number in List
- Python Second Largest in List
- Python Second Smallest in List
- Python Insert Element in List
- Python Delete Element from List
- Python Multiply Numbers in List
- Swap Two Elements in List
- Python 1D Array Program
- Python Linear Search
- Python Binary Search
- Python Insertion Sort
- Python Bubble Sort
- Python Selection Sort
- Remove Duplicates from List
- Python Reverse a List
- Python Merge Two List
- Python Copy a List
- Python Conversion Programs
- Python Decimal to Binary
- Python Decimal to Octal
- Python Decimal to Hexadecimal
- Python Binary to Decimal
- Python Binary to Octal
- Python Binary to Hexadecimal
- Python Octal to Decimal
- Python Octal to Binary
- Python Octal to Hexadecimal
- Python Hexadecimal to Decimal
- Python Hexadecimal to Binary
- Python Hexadecimal to Octal
- Python Matrix Programs
- Python Add Two Matrices
- Python Subtract Two Matrices
- Python Transpose Matrix
- Python Multiply Matrices
- Python String Programs
- Python Print String
- Python Find Length of String
- Python Compare Two Strings
- Python Copy String
- Python Concatenate String
- Python Reverse a String
- Python Swap Two Strings
- Python Uppercase to Lowercase
- Python Lowercase to Uppercase
- Python Check Substring in String
- Python Count Character in String
- Count Repeated Characters
- Python Count Word in Sentence
- Python Count Each Vowels
- Python Capitalize Character
- Python Capitalize Word in String
- Python Smallest/Largest Word
- Remove Spaces from String
- Remove Duplicate Character
- Remove Vowels from String
- Remove Punctuation from String
- Python Remove Word in String
- Python Remove Duplicate Words
- WhiteSpace to Hyphens
- Replace Vowels with Character
- Replace Character in String
- Python Sort String in Alphabetical
- Sort Word in Alphabetical Order
- Extract Number from String
- Python Check Anagram Strings
- Python File Programs
- Python Read a File
- Python Write to File
- Python Append Text to File
- Python Copy Files
- Python Merge Two Files
- Python Counts Characters in File
- Python Count Words in File
- Python File Content in Reverse
- Python Lines Contains String
- Python Delete Line from File
- Python Capitalize Word in File
- Python Replace Text in File
- Replace Specific Line in File
- Python Find Size of File
- Python List Files in Directory
- Python Delete Files
- Python Misc Programs
- Python Reverse a Tuple
- Python Merge Two Dictionary
- Python bytes to String
- Python bytearray to String
- Generate Random Numbers
- Python Print Address of Variable
- Python Print Date and Time
- Python Get IP Address
- Python Shutdown/Restart PC
- Python Tutorial
- Python Tutorial
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.
- Capitalize the first letter of each word using a list.
- Capitalize all words in a string using list and upper().
- Capitalize all words in a string using title().
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:
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:
- When the user enters the string, it gets stored in the text variable. So text="welcome to python"
- len(text) or len(welcome to python) or 17 gets initialized to textLen.
- Now the execution of the for loop begins. The range() method returns a sequence of values starting with 0 and incrementing by 1 by default. It stops before the value specified as its argument.
- At first execution, i=0 and the condition i<textLen or 0<17 evaluates to true; therefore, program flow goes inside the loop.
- text[i] or text[0] or w gets initialized to ch.
- The condition (of if) i==0 or 0==0 evaluates to true, therefore program flow goes inside the if's body.
- And ord(ch) or ord(w) or 119 (Unicode of w) gets initialized to ascVal.
- Now the first condition (of the second if) ascVal>=97 or 119>=97 evaluates to true, and the second condition (of the second if) ascVal<=122 or 119<=122 evaluates to true. Since both conditions evaluates to true, program flow goes inside the second if's body.
- ascVal-32 or 119-32 or 87 (the ASCII value of capital W) gets initialized to ascVal.
- chr(ascVal) or chr(87) or W gets initialized to ascVal.
- 0 gets initialized to index. And the following statement
text = text[:index] + ascVal + text[index+1:]
states that the character of the indexth or 0th index of text gets capitalized. - Now the condition (of another if) ch==" " gets evaluated, since ch's value is not equal to a space right now, therefore the condition evaluates to false, and the program flow goes back (to the for loop) and increments the value of i. That is, now i's value becomes 1 and checks whether it is less than textLen or not.
- Since the condition i<textLen or 1<17 evaluates to true again, program flow again goes inside the loop. This process continues until the condition is evaluates as false.
- In this way, each word of the given string gets capitalized.
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:
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() 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.
« Previous Program Next Program »