Computer >> Computer tutorials >  >> Programming >> Python

Python program for removing n-th character from a string?


String means array of character so starting address is 0.then we can easily get the index of every character. We have to input that index no. then remove that element. So split the string into two sub string. And two part should be one before n th indexed character and another after indexed character, the merged this two string.

Example

Input: python
n-th indexed: 3
Output: pyton

Explanation

Python program for removing n-th character from a string?

Algorithm

Step 1: Input a string.
Step 2: input the index p at the removed character.
Step 3: characters before the p-th indexed is stored in a variable X.
Step 4: Character, after the n-th indexed, is stored in a variable Y.
Step 5: Returning string after removing n-th indexed character.

Example code

# Removing n-th indexed character from a string
def removechar(str1, n): 
   # Characters before the i-th indexed is stored in a variable x
   x = str1[ : n] 
   # Characters after the nth indexed is stored in a variable y
   y = str1[n + 1: ]
   # Returning string after removing the nth indexed character.
   return x + y
# Driver Code
if __name__ == '__main__':
   str1 = input("Enter a string ::")
   n = int(input("Enter the n-th index ::"))
   # Print the new string
   print("The new string is ::")
   print(removechar(str1, n))

Output

Enter a string:: python
Enter the n-th index ::3
The new string is ::
pyton