Python Forum
len(bridge_knight) = 94 characters...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
len(bridge_knight) = 94 characters...
#1
bridge_knight = "I seek the bravest and the finest knights in the land who will join me in my court at Camelot."
When I run len(bridge_knight) the output is 94. So this identifies 0-93 totaling 94 characters; I get that. But what I don't get is the output using bridge_knight[:53]. Is that identifying it as 0-52? If so, then why do I get the space (character 53 in my string) when I use bridge_knight[53:94]?

I am struggling with the logic here:

bridge_knight[94] outputs string index out of range which is correct, there is no 94, it relies on 0-93.

So why do I get a period when I run bridge_knight[53:94] and not when I run bridge_knight[53:93]? The two sites I just deleted from my faves says it counts to 94 and includes it, which, when I run these codes, my eyes and brain de-myths (that is so not a word!).

bridge_knight[93] outputs "." which is the last of 94, that starting with 0. It makes perfect sense. So why do I need to run bridge_knight[##:94] to see it? Does the : mean up to 94, but not including like my brain and eyeballs are telling me?

I need to take a break, I've been at this computer for four hours writing code and completing this book; I need to clear my head. This is all confusing all of a sudden. Why do the Python's forsake me? Those sly, slithery snakes!
Reply
#2
Indexes are kind of like this:

Output:
I _ s e e k _ t h e... 0 1 2 3 4 5 6 7 8 9 10...
They are between the items, not on the items. If you slice with just one index, you get the one item after that index. If you slice with two indexes, you get what is between those two indexes. That's while text[y] is not in text[x:y]. The first is looking after the y, the second is looking up to the y. Also, if you slice with a second number, that number can be as large as you want and you won't get an index error. This is useful for trimming a bunch of strings to one maximum length.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jun-10-2017, 09:12 PM)ichabod801 Wrote: That's while text[y] is not in text[x:y]. The first is looking after the y, the second is looking up to the y.

Output:
>>> bridge_knight[53:93] ' who will join me in my court at Camelot' >>> bridge_knight[53] ' '
So why does 53 (the 'space') appear in output2, yet still (the 'space') in output1? I apologize for the delay, I took a few hours off to clear my head. It is the first number that confuses me the most. I understand the "up to" on the 'y', but the 'x', it counts as is, instead of right after like I am reading this stuff should happen. Am I missing something?

-Rod
Reply
#4
Index 53 is just before the space. So [53:93] gets everything from just before the space through index 93, so it includes the space. Plain [53] just gets the one item after index 53, which is the space.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
some_list[start:stop:step]
it is including start up to, but not including, stop
Reply
#6
(Jun-11-2017, 07:31 AM)buran Wrote: Plain [53] just gets the one item after index 53, which is the space.

This is where my logic failed me. I was reading (on two sites, and one book) that this included the exact item. I broke the string down into single characters (one per line) into Notepad++ and applied this logic. It makes sense now. 53: stand here, take one step forward; output.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,836 May-15-2020, 01:37 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020