I am currently reading ATBSWP and in one of the chapters the author writes a program that iterates through every 12 character(chunk) to figure out if there is phone number in the given text.
def is_phone_num(text):
if len(text) != 12:
return False
for i in range(0, 3):
if not text[i].isdecimal():
return False
if text[3] != '-':
return False
for i in range(4, 7):
if not text[i].isdecimal():
return False
if text[7] != '-':
return False
for i in range(8, 12):
if not text[i].isdecimal():
return False
return True
message = 'Call me at 415-555-1011 tomorrow.'
for i in range(len(message)):
chunk = message[i:i+12]
if is_phone_num(chunk):
My problem here is that this code works just fine. When I run this code, I am expecting to get a IndexError: string index out of range BECAUSE length of message is 60, when the for loop runs and z hits 55 (for example) then z+12 is going to be out of the range of message's length so how come I am not getting IndexError: string index out of range and the code runs perfectly?