Issue/bug/typo in the example of 6/10


#1

The example in this lesson (before the instructions) has a miscount error.

s = "Charlie"

print s[0]
# will print "C"

print s[1:4]
# will print "har"

Not a big deal, but I found it confusing for a second. Might be even more confusing for others. Either way, it's misleading and I think it should be fixed.


LIST SLICING: please help!
#2

Hi

I found your topic interesting so I ran the print s[1:4] using Charlie.

This was my code -

pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
new_word =word + first + pyg

s = new_word

print original
print word
print first
print new_word
print s[1:4]

else:
print 'empty'

This was the results of the prints I requested.

Enter a word: Charlie

Charlie
charlie
c
charliecay
har

The s[1:4] means # characters from position 1 to position 4. It seems the letter number after the : is excluded from being counted.

Maybe visit stack overflow.com, I found my answer there.

Hope this helped.


#3

That's interesting. Thank you for the clarification.


#4

No, there is no mistake.

print s[1:4]
It will slice the string beginning from the second character (first character is always 0) until before 4. so you get har printed out.


#5

I see. I assumed it meant "1 through 4" and included character number 4. My mistake.


#6

That is a very common misconception, I had that mistake too. No worries.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.