Python program for removing n-th character from a string?
Answers
Let's say we have a string and we will be taking the input.
sentence = input('Enter your sentence:')
Now, we will have to list the string. The list command in python lists each character in a list.
If we have a string called 'hi' and we list this string saying list(string), the output would be:
['h','i'] and from here on now, if we want to remove the nth character, we just use the list operation list.remove[nth number]
But now here is the catch,
If we have a list saying ['h','e','l','l','o'] and if we are asked to remove the second number, we will have to remove the 'e' but the index of the list of 'e' is 1 and not 2.
Because, for us, the numbers we count generally start from 1, first, second, third and etc but in python, it always starts from 0 and this is called the index.
In the list ['h','e','l','l','o'], the index of 'hi' is 0, index of 'e' is 1, index of 'l' is 2 and so on...
Now, we will have an input for the nth number and we will subtract one number for it and remove the character.
Now, here is the code:
sentence = list(input('Enter your sentence'))
number = int(input('Enter the nth number')) - 1
sentence.remove[number]
print(''.join(sentence))
And there we go, we have all the code now and it works perfectly alright!