EN
Python - remove character
0
points
In this article, we would like to show you how to remove character in Python.
Quick solution:
text = "ABC"
index = 1
# remove character at index 1
if len(text) > index:
text = text[0: index] + text[index + 1:]
print(text) # AC
1. Remove character at specified index
In this example, we use string slicing to remove character at index 1
from the text
.
text = "ABC"
index = 1
# remove character at index 1
if len(text) > index:
text = text[0: index] + text[index + 1:]
print(text) # AC
2. Remove first character
In this example, we use string slicing to remove the first character from the text
.
text = "ABC"
result = text[1:]
print(result) # BC
3. Remove last character
In this example, we use string slicing to remove the last character from the text
.
text = "ABC"
result = text[:-1]
print(result) # AB