You type A Ali into a school app. Does the space count as part of your name?
You type A Ali into a school app. On screen, the space looks like a gap between the letters. Does the program count that gap as part of your name? And how would you tell the program where to find it?
When you type A Ali, the program can store what you typed in the name field. It keeps the characters in the order you typed them. Text stored as a sequence of characters like this is called a string.
Follow the count from left to right. The first A is one, the space is two, and the next A is three. Lowercase L is four and lowercase I is five. That total is the string's length.
You type M Khan, with one space after the M. How many characters does the program store? Count through the string from left to right, then pick the length.
Now look at each character's position. In Python, the first A is at position zero, the space is at one, and the second A is at two. Lowercase L is at three and lowercase I is at four. These position numbers are called indices.
Start with A Ali stored in name. Len of name gives five because it counts every character. Name at index one gives the space because counting positions starts at zero. The last index is five minus one, so name at index four gives lowercase I.
Now name stores Sam Lee. Count every character to find the length, then use Python's positions to find the space and the final letter. Pick the option that gives the length, the space's index and the last index, in that order.
With A Ali in name, the slice gives you Ali, a substring. It reads only indices two, three and four. It's easy to think the slice includes five, but Python stops just before it. Join Hi with a trailing space to Ali to get Hi Ali. That joining is called concatenation.
A character has a number assigned to it in Unicode. In Python, ord of capital A gives its code, sixty-five. To convert back, use chr of sixty-five. That gives the character capital A again.