Languages
[Edit]
EN

Python - remove character

0 points
Created by:
Antonina-Trejo
237

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

Related posts

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

â€ïžđŸ’» 🙂

Join