0

I am new to Python and I‘m currently just messing around a little bit... but I am stuck with this one problem. I am trying to remove certain indexes from a string with a for loop. My Idea was something like this:

Text="Gvusaibdsz8audvbsauzdgsavuczisagbcsuzaicbhas"
for i in range(0,7):
    Text=Text.replace(Text[i], "")
print(Text)

But it removes only one index and is restoring the already replaced ones, for example:

1.loop: vusaibdsz8audvbsauzdgsavuczisagbcsuzaicbhas
2.loop: Gusaibdsz8audvbsauzdgsavuczisagbcsuzaicbhas
1
  • 1
    variables should not start with uppercase chars :) Commented Nov 14, 2019 at 14:34

4 Answers 4

1

Surely, there is many ways to get the desired result. Below, there is a good one based on your logic (using a for loop). As you are replacing a character by an empty character, It is better to directly remove the desired character.

In the code, I have transformed the text into a list for easy handling. After that, I have removed all characters based on the indexes. Finally, a join operation yields the desired text.

text = "Gvusaibdsz8audvbsauzdgsavuczisagbcsuzaicbhas"
text_list = list(text)
for index in range(0, 7):
    text_list.remove(text[index])

text = ''.join(text_list)
print(text)   # dsz8audvbsauzdgsavuczisagbcsuzaicbhas
Sign up to request clarification or add additional context in comments.

Comments

1

If I understand what you are trying to do, you are trying to omit letters of the string Text that are in indexes 0,1,2,3,4,5 and 6. but your code doesn't do that currently, but indeed it will take the first letter which is G and it will remove it from all the string(there is one occurrence), the next loop Text is equal to u because Text is equal vusaibdsz8audvbsauzdgsavuczisagbcsuzaicbhas after omitting G in the first iteration, as there are five occurrences of u, Text will be equal to vsaibdsz8advbsazdgsavczisagbcszaicbhas and so on..

you can put print(Text) inside the for loop and watch the results:

>>> Text="Gvusaibdsz8audvbsauzdgsavuczisagbcsuzaicbhas"
>>> for i in range(0,7):
...     Text=Text.replace(Text[i], "")
...     print(Text)
... 
vusaibdsz8audvbsauzdgsavuczisagbcsuzaicbhas
vsaibdsz8advbsazdgsavczisagbcszaicbhas
vsibdsz8dvbszdgsvczisgbcszicbhs
vsidsz8dvszdgsvczisgcszichs
vidz8dvzdgvczigczich
viz8vzgvczigczich
viz8vzvcziczich

In Python you can do that without a loop and the best way for that by using slicing as follows:

Text = Text[7:]

This will give you Text equal to dsz8audvbsauzdgsavuczisagbcsuzaicbhas.

If your goal is to reach this through a loop(supposing you are in need of Text in every iteration), you can try this:

>>> Text="Gvusaibdsz8audvbsauzdgsavuczisagbcsuzaicbhas"
>>> for i in range(0,7):
...     Text = Text[1:]
...     print(Text)
... 
vusaibdsz8audvbsauzdgsavuczisagbcsuzaicbhas
usaibdsz8audvbsauzdgsavuczisagbcsuzaicbhas
saibdsz8audvbsauzdgsavuczisagbcsuzaicbhas
aibdsz8audvbsauzdgsavuczisagbcsuzaicbhas
ibdsz8audvbsauzdgsavuczisagbcsuzaicbhas
bdsz8audvbsauzdgsavuczisagbcsuzaicbhas
dsz8audvbsauzdgsavuczisagbcsuzaicbhas

I hope this will help!

Comments

0

Take a look at string slicing:

text = "Gvusaibdsz8audvbsauzdgsavuczisagbcsuzaicbhas"
newtext = text[1:]
print(newtext)

--> "Gvusaibdsz8audvbsauzdgsavuczisagbcsuzaicbhas"
--> "vusaibdsz8audvbsauzdgsavuczisagbcsuzaicbhas"

Comments

0

You can replace

Text=Text.replace(Text[i], "")

with

Text = Text[:i] + Text[i+1:]

which uses slicing to rebuild a string without the specific index.

Side note: variable names should be lower case.

2 Comments

Great thanks a lot
That's wonderful. Could you accept this answer by clicking the checkmark.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.