Advertisements
Advertisements
प्रश्न
Write a user-defined function to convert a string with more than one word into a title case string where the string is passed as a parameter. (Title case means that the first letter of each word is capitalized).
थोडक्यात उत्तर
Advertisements
उत्तर
Program:
#Changing a string to title case using title() function
def convertToTitle(string):
titleString = string.title();
print("The input string in title case is:",titleString)
userInput = input("Write a sentence: ")
#Counting the number of space to get the number of words
totalSpace = 0
for b in userInput:
if b.isspace():
totalSpace += 1
#If the string is already in title case
if(userInput.istitle()):
print("The String is already in title case")
#If the string is not in title case and consists of more than one word
elif(totalSpace > 0):
convertToTitle(userInput)
#If the string is of one word only
else:
print("The String is of one word only")
OUTPUT:
Write a sentence: good evening!
The input string in title case is: Good Evening!shaalaa.com
String Method and Built-in Functions in Python
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
