Write a Python program to removes the last N characters from a string

Here is a problem going to be solve in given string having a number of characters but is going to be remove the last N characters from a string using python.

In logical words given a string S and an integer N, the task is to remove N characters from the end of the string S.

Input: S = “LingarajTechhub”, N = 5
Output: LingarajTe
Explanation: Removing the last 5 characters from “LingarajTechhub” modifies the string to “LingarajTe”.

Input: S = “Welcome”, N = 3
Output: Welc

1st Algorithm removes the last N characters from a string:

Follow the steps below to solve the problem:

Step-1: Initialize an empty string, say res, to store the resultant string.

Step-2: Iterate over the characters of the string S up to index len(S) – N.

Step-3: Then at last the removing characters store on res.

Below is the implementation of the above approach:

# Python3 code for the above approach

# Function to remove last N
# characters from string
def removeLastN(S, N):
	
	# Stores the resultant string
	res = ''
	
	# Traverse the string
	for i in range(len(S)-N):
		
		# Insert current character
		res += S[i]

	# Return the string
	return res

	
	
# Driver Code

# Input
S = "LingarajTechhub"
N = 5

print(removeLastN(S, N))

Output:

LingarajTe

Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N)

2nd Algorithm removes the last N characters from a string:

Now we follow the approach to solve this problem using replace() method with help of the following steps:

Step-1: Initialize a string, say res, to store the resultant string.

Step-2: Then we do reverse the string.

Step-3: Using replace() method, remove the first occurrence of the first characters of S and store it in res.

Step-4: Reverse the string res.

Below is the implementation of the above approach:

# Python3 code for the above approach

# Function to remove last N
# characters from string S
def removeLastN(S, N):
	
	# Stores resultant string
	res = ''
	
	# Reversing S
	S = S[::-1]
	
	# Removing last N characters
	res = S.replace(S[:N], '', 1)
	
	# Reversing back res
	res = res[::-1]
	
	# Return the string
	return res

# Driver Code

# Input
S = "LingarajTechhub"
N = 5

print(removeLastN(S, N))

Output:

LingarajTe

Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N)

3rd algorithm removes the last N characters from a string:

In this algorithm we are using String Slice method or approach to solve the problem.

  • Step-1: Initialize a string, say res, to store the resultant string.
  • Step-2: Update res to S[:len(S) – N], to store all characters except the last N characters of S.

Below is the implementation of the above approach:

# Python3 code for the above approach

# Function to remove last N
# characters from string S
def removeLastN(S, N):
	
	S = S[:len(S)-N]
	
	# Return the string
	return S

# Driver Code

# Input
S = "LingarajTechhub"
N = 5

print(removeLastN(S, N))

Output:

LingarajTe

Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)

Please comment and share about this article and if you want to improve this article please WhatsApp us.

Share Your Love
Default image
Lingaraj Senapati
Hey There! I am Lingaraj Senapati, the Co-founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.
Articles: 136

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply