Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3 string slicing
#1
I am trying to learn Python3 and have come across Lists. I know that counting begins at 0 and ends at -1
But i am having some difficulty in understanding the following example:

S[6:1:-2] = A B C D E F G H I

Could someone please explain what is going on here please.
Reply
#2
The following code should help you (do additional tests by yourself):
Alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

# extract from 6th term  to 1st one (excluded) every 2 terms ("-" means opposite sens i.e. from right to left)
Extract = Alphabet[6:1:-2]
print(f"Extract = {Extract}")
Output:
Extract = ['G', 'E', 'C']
Reply
#3
(Feb-16-2024, 08:06 AM)Luchano55 Wrote: counting begins at 0 and ends at -1
That is not correct statement. The correct is that you can use both positive and negative indexes.

spam = list(range(10))
print(spam)
print(spam[:100])
print(spam[-1::-2])
print(spam[-5:])
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [9, 7, 5, 3, 1] [5, 6, 7, 8, 9]

(Feb-16-2024, 08:06 AM)Luchano55 Wrote: S[6:1:-2] = A B C D E F G H I

Could someone please explain what is going on here please.

Start from element at index 6, and go to element at index (not including) 1, using step -2, i.e. every other one
print(spam[6:1:-2])
Output:
[6, 4, 2]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
This is not a valid python expression.
Quote:S[6:1:-2] = A B C D E F G H I
Was it supposed to be this?
S[6:1:-2] = A, B, C, D, E, F, G, H, I
This is a valid expression, but there is an error. S[6:1:-2] is a slice consisting of S[6], S[4] and S[2]. You cannot assign A, B, C, D, E, F, G, H and I to a slice of length 3.

This is valid, assuming len(S) >6 and that A, B and C are initialized variables.
S[6:1:-2] = A, B, C
Reply
#5
Maybe this will help?

from string import ascii_lowercase, ascii_uppercase

# quick way to get the alphabet
alphabet = ascii_lowercase
# get every second letter, starting at alphabet[13]
# won't fetch alphabet[1]
myslice = alphabet[13:1:-2]
mylist = [i for i in alphabet]
# get every second letter, starting at mylist[13]
# won't fetch mylist[1]
mylistslice = mylist[13:1:-2]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,576 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  convert string into multiple columns in python3 VAN 2 2,775 Sep-26-2020, 11:14 PM
Last Post: scidam
  String slicing and loop iteration divyansh 9 4,760 Jun-07-2020, 10:29 PM
Last Post: divyansh
  String slicing divyansh 6 3,379 May-31-2020, 06:15 AM
Last Post: pyzyx3qwerty
  string slicing help oli_action 2 2,206 Mar-22-2020, 09:57 AM
Last Post: oli_action
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,927 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  Accepting strings as arguments when slicing a string? (Ziyad Yehia on Udemy) Drone4four 4 3,804 Aug-23-2019, 07:59 PM
Last Post: Drone4four
  string slicing default value Uchikago 1 2,833 Jul-01-2019, 11:19 AM
Last Post: perfringo
  string slicing Uchikago 2 2,409 Jun-28-2019, 06:35 AM
Last Post: perfringo
  python3 List to array or string to extract data batchenr 4 3,281 May-28-2019, 01:44 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020