Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Given a string(sentence) the task is to Print only the First Letter of Each Word in Capital Letters Separated by a Full Stop in Python.
Examples:
Example1:
Input:
Given string =hello this is btechgeeks 21 python
Output:
H.T.I.B.P.
Example2:
Input:
Given string =good morning this is btechgeeks online coding 24 platform for btech students
Output:
G.M.T.I.B.O.C.P.F.B.S.
- Capitalize First Letter of each Word in a String in Java | Camel Case Naming Conventions in Java
- Python Program to Print First k Digits of 1/N where N is a Positive Integer
- Python Program to Split a String at First Instance of a Character
Program to Accept a Sentence and Print only the First Letter of Each Word in Capital Letters Separated by a Full Stop in Python
Below are the ways to Print only the First Letter of Each Word in Capital Letters Separated by a Full Stop in Python.
Method #1: Using For Loop (Static Input)
Approach:
- Give the sentence/string as static input and store it in a variable.
- Split the sentence into a list of words using the built-in split() function.
- Take an empty string to say resultstrng and initialize with the null value using “”.
- Traverse in this list of words using the For loop.
- Check if the word is alphabet or not using the If statement and isalpha() function
- If it is true then, Convert the first letter of the word of the string to the upper using the upper function and store it in a variable.
- Concatenate dot character to the above variable using string concatenation.
- Add this result to resultstrng using string concatenation.
- Print resultstrng.
- The Exit of the Program.
Below is the implementation:
# Give the sentence/string as static input and store it in a variable. gvnstrng = 'hello this is btechgeeks 21 python' # Split the sentence into a list of words using the built-in spit() function. gvnstrngwords = gvnstrng.split() # Take an empty string to say resultstrng and initialize with the null value using "". resultstrng = "" # Traverse in this list of words using the For loop. for strngword in gvnstrngwords: # Check if the word is alphabet or not using the If statement and isalpha() function if(strngword.isalpha()): # If it is true then, Convert the first letter of the word of # the string to the upper using the upper function and store it in a variable. firstchar = strngword[0].upper() # Concatenate dot character to the above variable using string concatenation. firstchar = firstchar+"." # Add this result to resultstrng using string concatenation. resultstrng = resultstrng+firstchar # Print resultstrng. print(resultstrng)
Output:
H.T.I.B.P.
Method #2: Using For Loop (User Input)
Approach:
- Give the sentence/string as user input using the input() function and store it in a variable.
- Split the sentence into a list of words using the built-in split() function.
- Take an empty string to say resultstrng and initialize with the null value using “”.
- Traverse in this list of words using the For loop.
- Check if the word is alphabet or not using the If statement and isalpha() function
- If it is true then, Convert the first letter of the word of the string to the upper using the upper function and store it in a variable.
- Concatenate dot character to the above variable using string concatenation.
- Add this result to resultstrng using string concatenation.
- Print resultstrng.
- The Exit of the Program.
Below is the implementation:
# Give the sentence/string as user input using the input() function # and store it in a variable. gvnstrng = input('Enter some random string/sentence = ') # Split the sentence into a list of words using the built-in spit() function. gvnstrngwords = gvnstrng.split() # Take an empty string to say resultstrng and initialize with the null value using "". resultstrng = "" # Traverse in this list of words using the For loop. for strngword in gvnstrngwords: # Check if the word is alphabet or not using the If statement and isalpha() function if(strngword.isalpha()): # If it is true then, Convert the first letter of the word of # the string to the upper using the upper function and store it in a variable. firstchar = strngword[0].upper() # Concatenate dot character to the above variable using string concatenation. firstchar = firstchar+"." # Add this result to resultstrng using string concatenation. resultstrng = resultstrng+firstchar # Print resultstrng. print(resultstrng)
Output:
Enter some random string/sentence = good morning this is btechgeeks online coding 24 platform for btech students G.M.T.I.B.O.C.P.F.B.S.
Related Programs:
- python program to read a file and capitalize the first letter of every word in the file
- python program to accept a hyphen separated sequence of words as input and print the words in a hyphen separated sequence after sorting them alphabetically
- python program to print each word of a sentence along with number of vowels in each word
- python capitalize the first letter of each word in a string
- python program to sort a list of tuples in increasing order by the last element in each tuple
- python program to calculate the number of digits and letters in a string
- convert first letter of each word of a string to upper case in cpp