Capitalize first letter of all the words in a string in Python

In this tutorial, we will learn how to capitalize the first letter of all the words in a string using Python. This is a very simple tutorial. we are going to solve this problem shortly.

First letter capitalization

For the first letter capitalization, we have many approaches. To solve this problem we have to import the String library from Python modules. String library is a built-in library in Python we have to import it. This library provides some methods and classes to do operations on Strings.

import string
str=input("enter the string:")
string.capwords(str)

Here we have used the string.capwords() method .which capitalize the all letters which are next to space. The letter next to space capital by the method.The method itterate over the string and capitalize the all first letters of words.

output:

enter the string:python is best Language to learn for begainers

Here we have to enter the string or sentence. For example we have taken the string as shown.

'Python Is Best Language To Learn For Begainers'

By this way, in very short we can Capitalize the string.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top