How To Capitalize First Letter In Python
Last Updated : Apr 9, 2023
IN - Python
In this article we will show you the solution of how to capitalize first letter in python, a specified input string's initial character is changed to uppercase by the capitalization() method in Python. The string's remaining characters are all changed to lowercase.
It is possible to make headlines using this form of case conversion, which is comparable to utilising sentence case within word processing software like Microsoft Word.
The Python String capitalize() function copies the original string as well as changes the first character to an uppercase (capital) letter while changing the rest of the characters inside the string to lowercase letters.
Now let's discuss the idea of capitalising the initial letter in Python.
Step By Step Guide On How To Capitalize First Letter In Python :-
Method - Capitalize in Python
inputStr = "pyThoN" capitalizedStr = inputStr.capitalize() print ("Original string: " + inputStr) print ("String after capitalize(): " + capitalizedStr)
- As you can see, we used Python's capitalise function to write the code necessary to capitalise the first letter in this sentence.
- The capitalization() function in Python raises and lowercases the first character of a string.
- The changed string is returned by the function, while the original input string is left alone.
- The capitalize() function doesn't require any inputs. We change a string or variable by calling the capitalize() function.
- The string that the capitalize() method produces. It takes a string as input, changes the initial character to upper case and the last character to lower case, and then outputs the transformed string.
- The capitalize() function is used in this code to capitalise the string "pyThoN."
- Then On the string "pyThoN," the capitalise() method is used.
- It generates a fresh string and stores it in the capitalizedStr variable.
- The input string inputStr's initial character, p, is changed to uppercase in this new string, while the following characters, yThoN, are changed to lowercase.
- That produces "Python" as our final output.
- The result is delivered as a new string capitalizedStr, but the initial string inputStr is left untouched.
Method - Python substitute for the capitalize() function
inputStr = "hEy theRe, WeLcOme tO MuMbAi!" capitalizedStr = inputStr[0:1].upper() + inputStr[1:].lower() print ("Original string:\n" + inputStr) print ("\nString after capitalize():\n" + capitalizedStr)
- Here, you can observe that Using the idea of string slicing & additional Python techniques like.upper() and.lower(), we can also mimic the functionality of capitalise() ().
- Using Python's.upper() function, we extract the first character from the input string by using subscript [0:1], and then we change the input string's case using the.upper() function.
- The following step is to retrieve the last few characters from the input string via [1:] and then use the.lower() function to change their case to lowercase.
- The output is then saved in a new variable after the two substrings have been concatenated.
- Although we are aware of the internal workings of the capitalise() function, it is recommended that you use it to simplify, condense, and improve the readability of your code.
The idea of capitalising the initial letter in Python has therefore been successfully mastered.
We also discovered how to utilise Python's capitalise() function to change a string's first character from lowercase to uppercase and vice versa.
The capitalise() function doesn't require any inputs. Although it returns a new, updated string, capitalise() does not change the original input string.
I hope this article on how to capitalize first letter in python helps you and the steps and method mentioned above are easy to follow and implement.