Python Program Enter ‘*’ Between two Identical Characters in a String

In the previous article, we have discussed Python Program to invert a Dictionary
Given a String and the task is to enter ‘*’ between two identical characters.

Example :

Given string = btechgeeks

Output: btechge*eks

Explanation: Since both the e’s are identical and next to each other. so, we enter a ‘*’ in between them.

Examples:

Example1:

Input:

Given string = "good morning btechgeekss"

Output:

The given string after entering '*' between two identical characters= go*od morning btechge*eks*

Example 2:

Input:

Given string = "aabcddeefghii"

Output:

The given string after entering '*' between two identical characters= a*abcd*de*efghi*

Program Enter ‘*’ Between two Identical Characters in a String

Below are the ways to enter ‘*’ between two identical characters in a given String.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Take a new empty string say ‘new_str’and store it in another variable.
  • Take a variable and initialize its value with ‘0’ and store it in another variable.
  • Loop from ‘0’ to the length of the given string -1 using for loop and len() function.
  • Concat the new_str with the iterator value of the given string and store it in the same variable ‘new_str’.
  • Check if the iterator value of the given input string is equal to the iterator+1 value of the given input string using the if conditional statement.
  • If the statement is true, concatenate the ‘new_str’ with the ‘*’ symbol and store it in the same variable ‘new_str’.
  • Print the variable ‘new_str’ to enter ‘*’ between two identical characters in a given String.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "good morning btechgeekss"
# Take a new empty string say 'new_str'and store it in another variable.
new_str = ""
# Take a variable and initialize its value with '0' and store it in another variable.
itr = 0
# Loop from '0' to the length of the given string -1 using for loop and len() function.
for itr in range(0, len(gvn_str)-1):
    # Concat the new_str with the iterator value of the given string and
    # store it in the same variable 'new_str'
    new_str = new_str + gvn_str[itr]
# Check if the iterator value of the given input string is equal to the iterator+1 value
# of the given input string using the if conditional statement.
    if(gvn_str[itr] == gvn_str[itr+1]):
      # If the statement is true, concat the 'new_str' with the '*' symbol and
      # store it in the same variable 'new_str'.
        new_str += '*'
# Print the variable 'new_str' to enter '*' between two identical characters in a
# given String.
print("The given string after entering '*' between two identical characters=", new_str)

Output:

The given string after entering '*' between two identical characters= go*od morning btechge*eks*

Method #2: Using For Loop (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Take a new empty string say ‘new_str’and store it in another variable.
  • Take a variable and initialize its value with ‘0’ and store it in another variable.
  • Loop from ‘0’ to the length of the given string -1 using for loop and len() function.
  • Concat the new_str with the iterator value of the given string and store it in the same variable ‘new_str’.
  • Check if the iterator value of the given input string is equal to the iterator+1 value of the given input string using the if conditional statement.
  • If the statement is true, concatenate the ‘new_str’ with the ‘*’ symbol and store it in the same variable ‘new_str’.
  • Print the variable ‘new_str’ to enter ‘*’ between two identical characters in a given String.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Take a new empty string say 'new_str'and store it in another variable.
new_str = ""
# Take a variable and initialize its value with '0' and store it in another variable.
itr = 0
# Loop from '0' to the length of the given string -1 using for loop and len() function.
for itr in range(0, len(gvn_str)-1):
    # Concat the new_str with the iterator value of the given string and
    # store it in the same variable 'new_str'
    new_str = new_str + gvn_str[itr]
# Check if the iterator value of the given input string is equal to the iterator+1 value
# of the given input string using the if conditional statement.
    if(gvn_str[itr] == gvn_str[itr+1]):
      # If the statement is true, concat the 'new_str' with the '*' symbol and
      # store it in the same variable 'new_str'.
        new_str += '*'
# Print the variable 'new_str' to enter '*' between two identical characters in a
# given String.
print("The given string after entering '*' between two identical characters=", new_str)

Output:

Enter some random string = aabcddeefghii
The given string after entering '*' between two identical characters= a*abcd*de*efghi*

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.