Python Program to Accept a Sentence and Print only the First Letter of Each Word in Capital Letters Separated by a Full Stop

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.

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: