Python Program to Retain first N Elements of a String and Replace the Remaining by K


In this article we are going to learn how to use internla python functions like len(), slicing(), replace(), ljust() to retain first N element of a string and replace the remaining by k. The python strings are easily made by simply surrounding letters in quotations. Python treats single quotes and double quotes the same way. Assigning a value to a variable and creating a string is really straightforward.

Example

Assume we have taken an input string N, K values. We will now retain the first N characters of an input string and replace it with the given K character using the above methods.

Input

inputString = 'tutorialspoint'
input_N = 9
input_K = "#"

Output

Retaining N{9} characters and replacing rest with '#': tutorials#####

In the above example, the first N(9) characters' tutorials of an input string are retained and the remaining characters are replaced with input K i.e, # symbol.

Using * operator, slicing and len() function

In this method we are going to use * operator and internal functions like slicing and len() to retaining the first N elements of a string and then replacing the remaining with K. Here, len() function is the number of items in an object is returned by the len() method.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a variable to store the input string.

  • Print the input string.

  • Create another variable to store the input N number of elements to be retained.

  • Create another variable to store the input character K to be replaced with the rest of the other elements.

  • Slice through the first N characters of the string to retain the first N elements.

  • Multiply the given character by the remaining number of elements left.

  • Concatenate the above two strings with the + operator.

  • Print the resultant string after retaining input N elements and replacing the remaining with the input k character.

Example

The following program returns a string after retaining input N elements and replacing the remaining with the input k character using the * operator, slicing, and len() function –

# input string
inputString = 'tutorialspoint'
# printing input string
print("Input string:", inputString)
# input number of elements to be retained
input_N = 9
# input character to be replaced with rest of other elements
input_K = "#"
# Slicing till the input N index to retain the first N elements
# Multiplying the remaining number of elements with the replaced character
resultantStr = inputString[:input_N] + input_K * (len(inputString) - input_N)
# printing resultant string after retaining and replacing with the input character
print("Retaining N{9} characters and replacing rest with '#':", resultantStr)

Output

On executing, the above program will generate the following output –

Input string: tutorialspoint
Retaining N{9} characters and replacing rest with '#': tutorials#####

Using slicing, ljust() and len() functions

In this method, we are going ot use internal python functions like slicing, ljust() and len() to perform the given task.

Syntax

string.ljust(length, character)

Here, ljust() function is use to left align the string with the given character as the fill character. The space is the default character.

Example

The following program returns a string after retaining input N elements and replacing the remaining with the input k character using slicing, ljust() and len() functions –

# input string
inputString = 'tutorialspoint'
# printing input string
print("Input string:", inputString)
# input the number of elements to be retained
input_N = 9
# input character to be replaced with rest of other elements
input_K = "#"
# Slicing till the input N index to retain the first N elements
# Applying ljust() function to add remaining characters with the replaced character
resultantStr = inputString[:input_N].ljust(len(inputString), input_K)
# printing resultant string after retaining and replacing with the input character
print("Retaining N{9} characters and replacing rest with '#':", resultantStr)

Output

On executing, the above program will generate the following output –

Input string: tutorialspoint
Retaining N{9} characters and replacing rest with '#': tutorials#####

Using replace() function

In this method, we are going to use replace function of python to retain the first N element of a string and replace the remaining by K.

Syntax

string.replace(old, new, count)

Here replace() function returns a copy of the string that replaces all occurrences of an old substring with another new substring.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Create function replaceString() that retains the first N Elements of a string and replace the remaining elements with input K by accepting the input string as an argument.

  • Replace the remaining characters with the given k character using the replace() function.

  • Return the resultant string after retaining and replacing it with the input character.

  • Create a variable to store the input string.

  • Print the input string.

  • Create another variable to store the input N number of elements to be retained.

  • Create another variable to store the input character K to be replaced with the rest of the other elements.

  • Call the above-defined replaceString() function by passing the input string as an argument to it.

Example

The following program returns a string after retaining input N elements and replacing the remaining with the input k character using replace() function –

# creating a function that retains the first N Elements of a string and
# replace the remaining with K by accepting the input string as an argument
def replaceString(inputString):
    # Replacing the remaining characters with the given k character
    resultantStr = inputString.replace(
        inputString[input_N:], input_K*len(inputString[input_N:]))
    # returning resultant string after retaining and replacing with the input character
    return str(resultantStr)

inputString = 'tutorialspoint'
# printing input string
print("Input string:", inputString)
# input number of elements to be retained
input_N = 9
# input character to be replaced with rest of other elements
input_K = "#"
print("Retaining N{9} characters and replacing rest with '#':")
# calling the above defined replaceString() function by passing the
# input string as an argument to it
print(replaceString(inputString))

Output

On executing, the above program will generate the following output –

Input string: tutorialspoint
Retaining N{9} characters and replacing rest with '#':
tutorials#####

Conclusion

This article has shown us three distinct ways to keep the first N elements of a string and replace the remaining K with another string. We learned how to add the elements to the end of the string using the ljust() method. Additionally, we learned how to create a character string with n elements by using the * operator to multiply a character by n.

Updated on: 18-Aug-2023

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements