Languages
[Edit]
EN

Python - remove character

0 points
Created by:
Antonina-Trejo
327

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

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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