Python program to capitalize the first letter of each word of a string :
In this tutorial, we will learn how to capitalize the first letter of each word in a string. The user will enter the string. Our program will save it in a variable. Then using the .title() method, we will convert the first letter of each word to capital and save the string in a different variable. Finally, the program will print out the result.
Before moving to the coding part, let me quickly show you what is the title() method of python string and what it does :
string.title() method :
This method is defined in the python string. We can use this method to capitalize the first letter of each word in a string. The syntax of the title method is:
s.title()
where ’s’ is the given string.
This method doesn’t take any parameter. As we know that string is immutable in python ,title() method will not modify the original string. Actually, it can’t. It will capitalize the start character of each word in the string and returns one new string.
If any word is starting with a number, it will remain the same.
For example, the string “hello world” will become “Hello World”. But string “1hello 2world” will remain the same as its words are starting with a number. It will be the same for any special characters.
title() method works a little bit differently with apostrophes. It will capitalize the first character after apostrophes if available in the string.
For example, “Don’t” will become “Don’T”.
So, be careful if you don’t want anything to be changed other than the start character.
Python program :
The implementation is simple. The program will ask the user to enter a string. It will read that string and call ‘title()’ on it. ‘title()’ will return the modified string and we will print out that string to the user.
Let’s take a look at the program :
Python program to capitalize the first letter of each word:
Explanation :
-
We are using the ‘input()’ method to read the user input values. It reads the input as a string. The value is stored in the ‘input_string’ variable.
-
Now, we are calling the ‘title()’ method on the ‘input_string” variable. It will capitalize each letter of the string ‘input_string’ and returns the modified string. We are storing the final string in ‘output_string’ variable.
-
Finally, we are printing out the final result to the user, i.e. we are printing out the contents of the ‘output_string’ variable.
Sample Output :
As you can see the first letter of each word is capitalized in the above output.
Conclusion :
title() method makes it easier to capitalize each character of a string in python. The only problem is that it will also capitalize the next character after an apostrophe. If you don’t want that and if you are confident that the string will not contain any apostrophes, you can use this method. Alternatively, you can use ‘regex’ to do the same task.
Try to run the program discussed above. As it is only a single method, you can also verify it on a python terminal. If you think that I have missed something or if you want to add anything to this post, drop one comment below.