write a python program to capitalize each word in a given string

Hi in this article you will learn about a Python program that capitalizes words present in the given string.  Given a string as input to the Python program to You have to write a Python logic to capitalize each word present in a string.

Let's look at this Python program.

 

 

write a python program to capitalize each word in a given string



write a python program to capitalize each word in a given string

Take a text variable and ask the user to interest the string using the input function call.

text = input("Enter a String : ")



Take an output variable and initialize it to an empty string,  use the for loop to iterate all the words present in the  string by using the split() function call.

Take each word, add it to output by converting each word into upper case using the built-in function upper() and add a space at the end to each word.

output = ""
for word in text.split():
   output += word.upper() + " "





Use the print function call to display a user friendly message and then print the output string .

print("String After Capitalize Each Word in a Given String is ...")
print(output)



Complete Python program
=========================

text = input("Enter a String : ")

output = ""
for word in text.split():
   output += word.upper() + " "

print("String After Capitalize Each Word in a Given String is ...")
print(output)




Output
===============

Enter a String : code with tj
String After Capitalizing Each Word in a Given String is ...
CODE WITH TJ



Extra Learning
===================

write a python program to capitalize first character of each word in a given string

In order to only capitalize the first character of each word in a string you just have to take the index of zero [0]. Capitalize the first letter of each word and add the remaining string using the string slicing method.

Complete Python Program
=============================

text = input("Enter a String : ")

output = ""
for word in text.split():
   output += word[0].upper() + word[1:] + " "

print("String After Capitalize First Character of Each Word in a String is ...")
print(output)




Output
==============

Enter a String : write a python program to capitalize each word in a given string
String After Capitalizing The First Character of Each Word in a String is ...
Write A Python Program To Capitalize Each Word In A Given String




Another Version using title() function call
================================================
write a python program to capitalize first character of each word in a given string


text = input("Enter a String : ")

output = text.title()

print("String After Capitalize First Character of Each Word in a String is ...")
print(output)




Conclusion
================

In the first Python program it rates all the world's present in the string and converts them to uppercase and adds them back to the output.  then finally prints the output string.

The second and third Python programs  are just for extra learning purposes to capitalize only the first letter in each word.

Comment down below if you have any queries regarding the above Python program.




Post a Comment

0 Comments