Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python I cannot slice Strings in a list

Kaworu

Active Coder
Hi!

I have a list of Strings whose contents begins with 5 characters, always the same, that I do not wanna. I want to get rid of them all for all the list individual contents.

My code is something like this:

Python:
for item in ENG_list:
    item = item[5:]

print(ENG_list)

Sadly, ENG_list, as printed, still contains the beginning characters. What do I do wrong?
 
You could do something like this
Python:
alist = ['12345ham', '12345spam', '12345spammy']

alist = [item[5:] for item in alist]

print(alist)

I would recommend making a copy to do the changes.
 
Last edited:
Hello there! Printing the string ENG_list wont show the sliced version after the for loop. It’ll show only the original version. I testing it to print “item” which seems to work but when you try to change how much to slice it still shows nothing. So i decided just to use the simplest one

Python:
ENG_list = "Hello"
    
print(ENG_list[5:])

the variable ENG_list has “Hello” in it. on the 2nd line the print will print ENG_list but sliced which will be 5 characters. Also keep in mind spaces count as characters

I hope this helped
 
Like they said above, you assigned the new item to a variable called item, which you should've appended onto a new list, and printed the original list. I hope this was useful!

Python:
ENG_list =["asdfhklll","asdfhklll","asdfhklll","asdfhklll"]
new_list = []
for item in ENG_list:
    item = item[5:]
    new_list.append(item)

#optional
ENG_list = new_list

print(ENG_list)
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom