Theory
One line, three answers
Your PocketMoney tracker just received a raw entry as one string:
2026-01-15 samosa 30
From that single line the program must pull out three separate answers: the year (2026), the item (samosa), and the amount (30). Nobody typed them in neat boxes; it is all one blob of text.
Pulling clean pieces out of a messy string is a daily job in real software, and Python gives you four small tools to do it. This page is those four tools.
Theory
A string is a row of numbered lockers
Picture the text samosa as a row of lockers, one character per locker, numbered from 0 on the left: s is locker 0, a is 1, m is 2, and so on. Python can hand you the contents of any locker by its number, or a whole stretch of lockers at once. Once you see a string as numbered lockers, indexing and slicing stop being mysterious: you are just naming locker numbers.
Theory
Indexing and length
Access one character with square brackets and a position, counting from 0:
word[0]is the first character.- Negative indexing counts from the right:
word[-1]is the last character,word[-2]the second last. Handy when you do not know the length.
len(word) returns how many characters the string has. A useful pairing: the last character is always word[len(word) - 1], which is exactly what word[-1] gives you the short way.
Practical
Indexing the item name
item = "samosa"
print(item[0]) # s (first locker)
print(item[-1]) # a (last locker, counting from right)
print(len(item)) # 6 (six characters)
print(item[len(item) - 1]) # a (same as item[-1], the long way)This example runs in Gri-Learn on the web, where you can edit it and see the output.
Theory
Slicing: a stretch of lockers
Slicing takes a run of characters with word[start:stop]. The golden rule, and the number one exam trap: the stop index is excluded. word[0:4] gives characters at 0, 1, 2, 3, but NOT 4.
So for our entry 2026-01-15 samosa 30:
- positions 0 to 3 hold
2026, sorecord[0:4]gives the year. - leaving out start means 'from the beginning', leaving out stop means 'to the end':
record[0:4]andrecord[:4]are the same.
Think first
Predict the slice
For word = "samosa", work out word[1:4] in your head before tapping. Remember: start is included, stop is excluded.
Show the answer
amo. Start at index 1 (a), include index 2 (m) and index 3 (o), then STOP before index 4 (s). So indices 1, 2, 3 give 'amo'. The most common wrong answer is 'amos', which happens when you forget that the stop index (4) is not included.
Practical
Parsing the PocketMoney entry
record = "2026-01-15 samosa 30"
year = record[0:4] # "2026" (positions 0,1,2,3)
last_char = record[-1] # "0" (rightmost character)
total_len = len(record) # 20 (counts spaces too)
print("Year:", year)
print("Length:", total_len)This example runs in Gri-Learn on the web, where you can edit it and see the output.
Theory
Concatenation: joining with +
The + operator glues strings together end to end:
"Rs. " + "30" gives "Rs. 30".
One strict rule Python will not bend: both sides of + must be strings. If one side is a number, you get a TypeError. The fix is str(), which turns a number into its text form:
"Total: " + str(30) gives "Total: 30".
This catches almost every beginner at least once.
Quiz
What does Python do with the expression `"Balance: " + 500`?
- It raises a TypeError, because you cannot add a number to a string
- It prints "Balance: 500" automatically
- It returns the number 505
- It ignores the number and prints "Balance: "
Show the answer
It raises a TypeError, because you cannot add a number to a string
Python does not silently convert types during concatenation: a string on the left and an integer on the right is a TypeError. Unlike some languages, it will not guess your intent. The fix is to wrap the number: "Balance: " + str(500). Option B is the trap for anyone expecting automatic conversion.
Watch out
Strings do not change in place
Every operation here reads the string or builds a new one; none of them edit the original. record[0:4] hands you a fresh piece and leaves record untouched, and you cannot do record[0] = "X" (that is a TypeError). Python strings are immutable. To 'change' a string you build a new one, for example new = "X" + record[1:]. Exams love to test this.
Theory
You have met this before, in C
In BCA104 a string was a char array ending in \0, and you found its length by looping until the \0, character by character. Python hides the \0 and hands you len(), slicing, and negative indexing for free. Same idea (text is a sequence of characters), far less manual work. Next you will meet the string METHODS (split, replace, upper), which turn these raw moves into one-word tools.
Summary
Key takeaways
- Indexing uses
word[i], starting at 0; negative indexing counts from the right, soword[-1]is the last character. - Slicing
word[start:stop]includes start but EXCLUDES stop; omit either end for 'from start' or 'to end'. - len(word) returns the character count, spaces included.
- Concatenation with + needs strings on both sides; wrap numbers in str() or get a TypeError.
- Strings are immutable: operations return new strings, they never edit the original.
- Memory hook: cut with [ : ], count with len, join with +, and stop is always one step short.