Write a script which converts 1st character of each line in a file to uppercase.
Answers
Answer:
Explanation:
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 :
1. We are using ‘input()’ method to read the user input values. It reads the input as a string. The value is stored in the ‘input_string’ variable.
2. 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.
3. 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.