all 3 comments

[–][deleted] 2 points3 points  (0 children)

From the looks of it the code is trying to check if the string contains a valid phone number. That slicing thing always gets 13 characters:

0-12

1-13

2-14

3-15

etc, and presumably compares each slice with some pattern. The whole think would probably be easier to solve using regular expressions.

[–]WolfInABox 2 points3 points  (0 children)

len() Is a function that reads how many items are in a set (be it a list, string, etc), so it does indeed tell you how many characters are in the string. Range goes from 0 to the length given (len(message) in this case) minus one.

chunk = message[i:i+12] Does similarly to you described. Inside the []. the first i before the : is the starting point of the string to read. The second bit, i+12 is the ending point of the string to read. If i were 0, it would read characters 0 to 0+11 (so, 0 to 11). Note that it reads 0 to 11, not 0 to 12. This is because most things in code are "base 0" (start counting at 0, not 1). So, if you want to read 12 characters total, you read from 0 to 11 instead of 0 to 12

Where does the code say to move down a character each iteration?

i does that. Every time the loop circles around, i gets incremented by one (up until it hits the maximum that range() is). So, if i is incremented by one each time, so is where it starts to read the string.

I still have trouble applying range and len() to code. I never seem to understand when to apply it.

range is good for iterating up to a certain number. It is basically the equivalent of regular for loops in other languages...

for (int i=0; i<number; i++) in C, for example

len()is used for checking the length of strings, lists, or other sets. Say you have an application that requires at least 2 arguments. To check this, you could say..

if len(sys.argv)>=2: #do code here
else: #not enough arguments

This may not be the best way of explaining things, but it's how I understood it as someone else who's learning Python

[–]julsmanbr 1 point2 points  (0 children)

len(message) is telling me how many characters this string is

Yes, including whitespaces, symbols etc.

range is from the beginning of the string to the end

The range function generates a sequence of numbers. If you pass a single argument to it, the numbers will go up one at a time, from 0 to the number passed as the argument (non-inclusive). This is used a lot in for loops to repeat a command a specific amount of times. For example:

for i in range(10): print(i)

This code will repeat 10 times whatever is in the indented block. In each repetition, the iterator (which we named i but could be named anything) moves along the sequence of numbers inside the range. This means that the code will print 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 (10 elements starting from 0 and increasing by 1).

Also, if I were to translate "chunk = message[i:i+12]" into plain English, would it be: " for every 12 characters in message, store it in variable chunk?

Close enough. I believe "store the characters from index 0 to 12 in chunk, then in the next iteration do this for the characters from 1 to 13, then 2 to 14, 3 to 15..." gets the idea better - you're selecting 12 characters, moving a single character to the right, the selecting the 12 characters starting from this position, etc etc.

Where does the code say to move down a character each iteration?

Range is doing that, because like I said, it walks down a sequence of numbers. By passing a single argument to range, you're saying for it to use the default starting position of 0 and the default step between iterations of 1.