All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Capitalize First Letter In Python

Last Updated : Mar 11, 2024

How To Capitalize First Letter 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)
  1. As you can see, we used Python's capitalise function to write the code necessary to capitalise the first letter in this sentence.
  2. The capitalization() function in Python raises and lowercases the first character of a string.
  3. The changed string is returned by the function, while the original input string is left alone.
  4. The capitalize() function doesn't require any inputs. We change a string or variable by calling the capitalize() function.
  5. 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.
  6. The capitalize() function is used in this code to capitalise the string "pyThoN."
  7. Then On the string "pyThoN," the capitalise() method is used.
  8. It generates a fresh string and stores it in the capitalizedStr variable.
  9. 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.
  10. That produces "Python" as our final output.
  11. 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)
  1. 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() ().
  2. 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.
  3. 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.
  4. The output is then saved in a new variable after the two substrings have been concatenated.
  5. 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.

Conclusion :-

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.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪