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