Syntax problem with [1:len(new_word)]?


#1



https://www.codecademy.com/courses/python-beginner-2W5v7/1/4?curriculum_id=4f89dab3d788890003000096#


Despite putting in my code precisely as the instructor says, the console tells me I have a syntax error, pointing to the colon.


Shouldn't this work fine? It's what the lesson told me to do!


new_word = [1:len(new_word)]


#2

Hello @lil.irrelevant,

The console is showing you a syntax error because you have enclosed code in square brackets, which tells python that you want to make an array, and arrays don't recognise the colon character unless it is inside of a string literal

However, what you are trying to go here is trying to extract every letter except for first out of the word variable. To do this, you must utilise string slicing, which works like this

word = "Hello, life!"

print word      # prints Hello, life!
print word[0]   # prints H
print word[3]   # prints l
print word[1:3] # prints el
print word[1:5] # prints ello

So, I hope this makes it clear what you were trying to do, and what you SHOULD be doing...


#3

Thank you @bartholomewallen,

This was helpful, but even without brackets I still get the syntax error. No matter what, the console points to the colon in the line as the source of syntax error.

Also, as a slight digression, how would I make the string slicing indefinite? As in, how would I say, "print everything but the first letter"?

According to the lesson, I would say print [1:].

Again, thank you very much for your advice and time.


#4

I know why u got this wrong, I was having the same problem until now. Notice how in the example is shows us how to write the slice, the variable is written b4 the brackets, to fix it, just type new_word or whatever it may be before [1:len(new_word)]

like this... new_word = new_word[1:len(new_word)]


#5

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