Python String Capitalize
As we have seen in the above table, the capitalize method returns the copy of the string, with the first character in uppercase, and the rest of the characters in lowercase. Once we learn to use the method and see the output, we would simply understand what the method has to do. One thing to note here is that the method here does not affect the original string.
When we are using the capitalize method on some string, a new string is created with the new changes, and the original string stays as it was. Let’s get to the example –
Python String capitalize() Method
As you can see in the above program, we have used the method on the string1. The way we did it is simple. We just have to write the variable name, which refers to the object on which we are using the method, followed by a dot, and then the method name, and do not forget the brackets there. This is similar to what we call some functions. But here, this function capitalize is being used on some string, so this is a method here, and you would soon get familiar with the way we are calling the methods.
Also, we are printing the output from the capitalize method, as well as the original string. We can simply find that the original string is as it was, and with the capitalization, we got a new string with the effective changes. We are directly printing the string, but you can assign it to some variable as well if you need it later.
If you have a look at the output for the above program, the capitalized string appears somewhat like this – ‘Hello from Gyanipandit’.
Even all the other characters that were in the uppercase in the string, were simply lowercase, and so we got the output. You can try this method as well, so as to become familiar with it.