To remove characters from string in python; In this tutorial, you will learn how to remove first, last, and specific characters from string in python.
Whenever you work in any programming language. So many times you need to remove characters, special characters, and substrings from the string that you have. Such as remove string first character, remove string last character, remove any specific character in the string, remove multiple characters from string and remove specific index characters from string in python, etc.
How to remove a character from a string python
See the following python programs to how to remove first, last and specific character from string in python:
- How to remove the last string character in a string.
- How to remove the first character from the Python string.
- Remove multiple characters from string in python.
- How to remove a character from a specified index in a string.
Python has many built-in functions/methods for manipulation with strings, which you can checkout python string methods.
1: How to remove the last string character in a string
# Python code to remove last character from string
#take input string from user
str = input("Please Enter String : ")
# Remove last character
str = str[:-1]
# Print remaining string
print(str)
After executing the above program, the output will be:
Please Enter String : my world.... Result :- my world...
2: How to remove the first string character in a string
# Python code to remove first character from string
# initializing test string
str='!my world'
# Remove first character
str = str[1:]
# Print remaining str
print(str)
After executing the above program, the output will be:
Please Enter String : A programmer Result :- programmer
3: Remove multiple characters from string in python
# Python code to remove multiple characters from string
#take input string from user
str = input("Please Enter String : ")
removeChr = input("Please Enter Multiple Character, which you want to remove : ")
# Remove multiple characters
new_string = str
for character in removeChr:
new_string = new_string.replace(character, "")
# Print remaining string
print('Result :- ', new_string)
After executing the above program, the output will be:
Please Enter String : !(Hell@o) Please Enter Multiple Character, which you want to remove : !()@ Result :- Hello
4: How to remove a character from a specified index in a string
# Python code to remove specific index character from string
#take input string from user
str = input("Please Enter String : ")
n = int(input("Please Enter Index of Character, which you want to remove : "))
# Remove nth character
x = str[:n] # up to but not including n
y = str[n + 1:] # n+1 till the end of string
str = x + y
# Print remaining string
print('Result :- ', str)
After executing the above program, the output will be:
Execution -1
Please Enter String : python Please Enter Index of Character, which you want to remove : 0 Result :- ython
Execution -2
Please Enter String : python Please Enter Index of Character, which you want to remove : 1 Result :- pthon
Recommended Python Tutorials