#๐ Simple coding that everyone can know I think but not me so dont hesitate to help
24 messages ยท Page 1 of 1 (latest)
@candid path
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
!slice
Slicing is a way of accessing a part of a sequence by specifying a start, stop, and step. As with normal indexing, negative numbers can be used to count backwards.
Examples
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters[2:] # from element 2 to the end
['c', 'd', 'e', 'f', 'g']
>>> letters[:4] # up to element 4
['a', 'b', 'c', 'd']
>>> letters[3:5] # elements 3 and 4 -- the right bound is not included
['d', 'e']
>>> letters[2:-1:2] # Every other element between 2 and the last
['c', 'e']
>>> letters[::-1] # The whole list in reverse
['g', 'f', 'e', 'd', 'c', 'b', 'a']
>>> words = "Hello world!"
>>> words[2:7] # Strings are also sequences
"llo w"
Please read embedded above. I think thatโll answer your question, but if not clear, ask more
It works fine at removing the first occurrence of the last digit
but the "2" is still there
Read what I wrote
ahhhhhh
sorry I missread
do you know what I should do ?
because I don't understand why
I put [-1]
so it should be the last two
You want all 2s gone? (If the last digit is 2?) Or just the last element
?
You can use a slice to get a new string without the last char
Then turn that string to a list
[-1] is the last and it is a 2.
Then you are looking through the list removing it the first time it is found.
letters = ['a', 'b', 'c', 'd']
print( letters[:-1] )
['a', 'b', 'c']
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.