Python Program to Print Pattern to Display Letters of the Word

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Given a word the task is to print the letters of the word in C, C++, and Python.

Examples:

Example1:

Input:

Given string ="BTechGeeks"

Output:

B
BT
BTe
BTec
BTech
BTechG
BTechGe
BTechGee
BTechGeek
BTechGeeks

Example2:

Input:

Given string ="Aplustopper"

Output:

A
Ap
Apl
Aplu
Aplus
Aplust
Aplusto
Aplustop
Aplustopp
Aplustoppe
Aplustopper

Pattern to display letters of the word in C, C++, and Python.

Below are the ways to display letters of the word in C, C++, and Python.

Method #1: Using For loop (Static Input)

Approach:

  • Give the word as static input and store it in a variable.
  • Take an empty string and store it in a variable say sampstrng.
  • Traverse the given word(string) using For loop.
  • Concatenate the iterator character to the sampstrng using string Concatenation.
  • Print the sampstrng.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the word as static input and store it in a variable.
givenword = 'BTechGeeks'
# Take an empty string and store it in a variable say sampstrng.
sampstrng = ""
# Traverse the given word(string) using For loop.
for wordchar in givenword:
    # Concatenate the iterator character to the sampstrng using string Concatenation.
    sampstrng = sampstrng+wordchar
    # Print the sampstrng.
    print(sampstrng)

Output:

B
BT
BTe
BTec
BTech
BTechG
BTechGe
BTechGee
BTechGeek
BTechGeeks

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the word as static input and store it in a
    // variable.
    string givenword = "BTechGeeks";
    // Take an empty string and store it in a variable say
    // sampstrng.
    string sampstrng = "";
    // Traverse the given word(string) using For loop.
    for (int wordchar = 0; wordchar < givenword.length();
         wordchar++) {
        // Concatenate the iterator character to the
        // sampstrng using string Concatenation.

        sampstrng = sampstrng + givenword[wordchar];
        // Print the sampstrng.
        cout << sampstrng << endl;
    }
    return 0;
}

Output:

B
BT
BTe
BTec
BTech
BTechG
BTechGe
BTechGee
BTechGeek
BTechGeeks

3) C Implementation

Below is the implementation:

#include <stdio.h>
#include <string.h>
int main()
{

    // Give the word as static input and store it in a
    // variable.
    char givenword[100] = "BTechGeeks";
    // Take an empty string and store it in a variable say
    // sampstrng.
    char sampstrng[100] = "";
    // Traverse the given word(string) using For loop.
    for (int wordchar = 0; wordchar < strlen(givenword);
         wordchar++) {
        // Concatenate the iterator character to the
        // sampstrng using string Concatenation.

        sampstrng[wordchar] = givenword[wordchar];
        // Print the sampstrng.
        printf("%s", sampstrng);
        printf("\n");
    }
    return 0;
}

Output:

B
BT
BTe
BTec
BTech
BTechG
BTechGe
BTechGee
BTechGeek
BTechGeeks

Method #2: Using For loop (User Input)

Approach:

  • Give the word as user input and store it in a variable.
  • Take an empty string and store it in a variable say sampstrng.
  • Traverse the given word(string) using For loop.
  • Concatenate the iterator character to the sampstrng using string Concatenation.
  • Print the sampstrng.
  • The Exit of the Program.

1) Python Implementation

Give the word as user input using input() and store it in a variable.

Below is the implementation:

# Give the word as user input and store it in a variable.
givenword = input('Enter some random word = ')
# Take an empty string and store it in a variable say sampstrng.
sampstrng = ""
# Traverse the given word(string) using For loop.
for wordchar in givenword:
    # Concatenate the iterator character to the sampstrng using string Concatenation.
    sampstrng = sampstrng+wordchar
    # Print the sampstrng.
    print(sampstrng)

Output:

Enter some random word = Aplustopper
A
Ap
Apl
Aplu
Aplus
Aplust
Aplusto
Aplustop
Aplustopp
Aplustoppe
Aplustopper

2) C++ Implementation

Give the word as user input using cin and store it in a variable.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the word as user input using cin and store it in
    // a variable.
    string givenword;
    cin >> givenword;
    // Take an empty string and store it in a variable say
    // sampstrng.
    string sampstrng = "";
    // Traverse the given word(string) using For loop.
    for (int wordchar = 0; wordchar < givenword.length();
         wordchar++) {
        // Concatenate the iterator character to the
        // sampstrng using string Concatenation.

        sampstrng = sampstrng + givenword[wordchar];
        // Print the sampstrng.
        cout << sampstrng << endl;
    }
    return 0;
}

Output:

Aplustopper
A
Ap
Apl
Aplu
Aplus
Aplust
Aplusto
Aplustop
Aplustopp
Aplustoppe
Aplustopper

3) C Implementation

Give the word as user input using scanf and store it in a variable.

Below is the implementation

#include <stdio.h>
#include <string.h>
int main()
{

    // Give the word as user input using scanfand store it
    // in a variable.
    char givenword[100];
    scanf("%s", givenword);
    // Take an empty string and store it in a variable say
    // sampstrng.
    char sampstrng[100] = "";
    // Traverse the given word(string) using For loop.
    for (int wordchar = 0; wordchar < strlen(givenword);
         wordchar++) {
        // Concatenate the iterator character to the
        // sampstrng using string Concatenation.

        sampstrng[wordchar] = givenword[wordchar];
        // Print the sampstrng.
        printf("%s", sampstrng);
        printf("\n");
    }
    return 0;
}

Output:

Aplustopper
A
Ap
Apl
Aplu
Aplus
Aplust
Aplusto
Aplustop
Aplustopp
Aplustoppe
Aplustopper

Related Programs: