Accept a sentence and print only the first letter of each word in capital letters separated by a full stop using functions in Python
In this Python tutorial, you will learn how to accept a sentence and print only the first letter of each word of the sentence in capital letters separated by a full stop.
We will ignore the words in which one or more characters are not in the alphabet.
We shall be using certain string functions in Python like isalpha(), split() and upper():-
- isalpha() method in Python returns True if all the characters in the string to which the function is applied to are alphabets, else it returns False.
- split() method in Python breaks up a sentence into its constituent words on the basis of a particular separator. Here we are separating on the basis of the spaces in between the words.
- The upper() function in Python is used to convert all the characters in the string to upper case.
Look at the input and output below to gain an idea of our required output:-
Python was created by Guido van Rossum and first released on February 20 1991
Here, the first letter of each word of the inputted sentence has been converted to its capital form. A full stop separates these letters.
We shall be writing a Python code to do the same using functions and utilizing Python String functions.
Code and Output in Python
Study the code in Python given below:-
#This is a function called 'multiplication' which returnsthe product of two numbers def first_letter_cap(word): if word.isalpha(): s = word.upper() + '.' return s else: return "" #Printing what the function 'multiplication' is returning sentence = "Covid 19 caused a global pandemic in 2020" list = sentence.split() req_string = "" for word in list: req_string = req_string + first_letter_cap(word) print(req_string)
Now, let’s move on to the explanation of the code!
Explanation of the Python code
- Take an argument ‘word’.
- If all the characters of ‘word’ is a letter, then we want to take the first character of this word, convert it to upper case and concatenate it with a “.” because that is our desired format. We store this in a string called s and return it.
- If all the characters of ‘word’ is not a letter, then we return an empty string “”.
Utilisation of the first_letter_cap() function in a loop:-
- Consider a sentence. In our case, it is “Covid 19 caused a global pandemic in 2020”. We use the split() function in Python to split up the sentences into a variable called ‘list’ consisting of its words. The list would look like this: [‘Covid’, ’19’, ’caused’, ‘a’, ‘global’, ‘pandemic’, ‘in’, ‘2020’]
- Consider an empty string ‘req_string’ where we will form the new string in the format that we require.
- Iterate over ‘list’ using a for loop and hence for each iteration, we have each element in list considered at the time in ‘word’. For example, in the first iteration, ‘word’ will be ‘Covid’, for the second iteration, ‘word’ will be ’19’ and so on.
- Concatenate ‘req_string’ with itself and what the function first_letter_cap() returns.
- After the for loop is over, print the string ‘req_string’.
I hope this tutorial was useful for you!