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_ .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 :
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 like below :
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.
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 :
- We are using_ ‘input()’_ method to read the user input values. It reads the input as a string. The value is stored in the ‘inputstring_’ variable.
- Now, we are calling the ‘title()’ method on the ‘inputstring” variable. It will capitalize each letter of the string ‘inputstring’ and returns the modified string. We are storing the final string in ‘outputstring_’ variable.
- Finally, we are printing out the final result to the user, i.e. we are printing out the contents of the ‘outputstring_’ variable.
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.
- Python program to find larger string among two strings
- Python program to count the frequency of each words in a string
- Python program to convert a string to an integer
- Python tutorial to calculate the sum of two string numbers
- Python program to pad zeros to a string
- Python program to replace single or multiple character,substring in a string