Can someone help with the len function

text = “Hello World”

print (text [len (text) -1] )

I’ve tried chat gpt and youtube tutorials and I’m not sure what I’m missing. It returns “d” on the console?

Welcome to the forum @nurdalert81

What are you trying to achieve?

The code you gave is giving the result of looking up the length of the string stored in the text variable.

Taking away one finds the last character of the text variable, which is d

H e l l o   W o r l d
0 1 2 3 4 5 6 7 8 9 10

Happy coding

1 Like

I’m taking a beginner course here on FCC and I can’t get past step 7 which is using the len function.

What are you trying to do with your code?

For print(text[len(text)-1]),

len(text)-1 returns the total number of characters in the string you provided minus by 1, which is 10.

Hence text[10], remember that a string is an array of characters, and that index starts from 0 by default, the character at the 10th position is ‘d’.

Happy learning

1 Like

Thank you , I added a screenshot of the course I’m taking here on FCC I can’t get past.

The instructions stated to pass in len(text) into the print call, could you do that?

I can’t wrap my head around what that looks like. Am I not doing it correctly?

Modify your existing print() call by passing len(text) instead of text[-1] .

You need to replace the existing code inside the print call.

I assume in the previous step, you were asked to print(text[-1]), returning the last character of the string, d.

However, what this step wants you to do is to simply print the length of the ‘Hello World’ string, and not asking you to return d, which is what text[len(text) - 1] achieves.

How would you go about printing the length of the string?

Yes, to your first question.

I think I get it now. Thank you all so much for the support!

I’ll give it another go, practice makes perfect.

Finally!
solution code removed

1 Like

Please do not post solution code, thanks