In the previous article, we have discussed Python Program to Find Common Characters between Two Strings
Given a String, the task is to repeat the string n times and add a separator in between.
Examples:
Example1:
Input:
Given string =hello this is BTechgeeks Given separator='-' n = 2
Output:
Given string with the above given separator = hello-this-is-BTechgeeks-hello-this-is-BTechgeeks
Example 2:
Input:
Given string =Good morning this is btechgeeks Given separator='@' n = 3
Output:
Given string with the above given separator = Good@morning@this@is@btechgeeks@Good@morning@this@is@btechgeeks@Good@morning@this@is@btechgeeks
Program to Repeat String N times with Separator
Below are the ways to Repeat String n times with Separator.
Method #1: Using split() Method (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Give the number as static input and store it in another variable.
- Multiply the Given string with the above-given number in order to get a repeated string and store it in another variable.
- Split the words of the given repeated string to a list of words using the built-in split() function and store it in another variable.
- Give the separator as static input and store it in another variable.
- Join the given separator in between the given Splitted String using the built-in join( ) function and store it in another variable.
- Print the given string with the above-given separator.
- The Exit of the program.
Note: Give an extra space at the end of the string while giving Static input.
Below is the implementation:
# Give the string as static input and store it in a variable. gven_strng = " hello this is BTechgeeks " # Give the number as static input and store it in another variable. multpr = 2 # Multiply the Given string with the above given number in order to get a repeated string # and store it in another variable. repetd_strg = gven_strng*multpr # Split the words of the given repeated string to a list of words using the built-in split() # function and store it in another variable. splitd_strng = repetd_strg.split() # Give the separator as static input and store it in another variable. separtr = '-' # Join the given separator in between the given Splitted String using the built-in join( ) # function and store it in another variable. reslt = separtr.join(splitd_strng) # Print the given string with the above given separator. print('Given string with the above given separator = ', reslt)
Output:
Given string with the above given separator = hello-this-is-BTechgeeks-hello-this-is-BTechgeeks
Method #2: Using split() Method (User Input)
Approach:
- Give the string as User input and store it in a variable.
- Give the number as User input and store it in another variable.
- Multiply the Given string with the above-given number in order to get a repeated string and store it in another variable.
- Split the words of the given repeated string to a list of words using the built-in split() function and store it in another variable.
- Give the separator as User input and store it in another variable.
- Join the given separator in between the given Splitted String using the built-in join( ) function and store it in another variable.
- Print the given string with the above-given separator.
- The Exit of the program.
Note: Give an extra space at the end of the string while giving User input.
Below is the implementation:
# Give the string as User input and store it in a variable. gven_strng = input('Enter some Random String = ') # Give the number as User input and store it in another variable. multpr = int(input('Enter some Random Number =')) # Multiply the Given string with the above given number in order to get a repeated string # and store it in another variable. repetd_strg = gven_strng*multpr # Split the words of the given repeated string to a list of words using the built-in split() # function and store it in another variable. splitd_strng = repetd_strg.split() # Give the separator as User input and store it in another variable. separtr = input('Enter some Random Separator = ') # Join the given separator in between the given Splitted String using the built-in join( ) # function and store it in another variable. reslt = separtr.join(splitd_strng) # Print the given string with the above given separator. print('Given string with the above given separator = ', reslt)
Output:
Enter some Random String = Good morning this is btechgeeks Enter some Random Number = 3 Enter some Random Separator = @ Given string with the above given separator = Good@morning@this@is@btechgeeks@Good@morning@this@is@btechgeeks@Good@morning@this@is@btechgeeks
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.
- Python Program to Check if a Substring is Present in a Given String
- Python Program to Check if a String is a keyword or Not
- Python Program to Check if a String is Lapindrome or Not
- Python Program to Determine Whether one String is a Rotation of Another