A string is a sequence of characters — text — written in quotes: "hello" or 'Vidaara'. You've already printed strings; now you'll learn to take them apart, search them, and transform them. Strings are everywhere in real programs, so this chapter pays off immediately — and it ends with you building a real Password Strength Checker.
1Indexing and immutability
Each character in a string has a position called its index. Indexing starts at 0, and you can also count backwards from the end with negative indices:
P y t h o n 0 1 2 3 4 5 (positive index) -6 -5 -4 -3 -2 -1 (negative index)
So for word = "Python": word[0] is 'P' and word[-1] is 'n'. The len() function gives the length (here, 6).
word[0] = 'J' causes an error. Instead, you build a new string from the old one. This is a favourite exam point.Click Run to execute.
- Indexing starts at 0; negative indices count from the end (-1 is the last character).
len(s)gives the number of characters.- Strings are immutable — you can't change a character in place; you build a new string instead.
2Slicing
Slicing takes a piece of a string with the syntax string[start:stop:step]. As with range(), the stop index is excluded:
word[0:3]→ characters 0, 1, 2 (not 3).word[2:]→ from index 2 to the end.word[:4]→ from the start up to (not including) index 4.word[::-1]→ the whole string reversed (a neat trick).
Slicing always returns a new string (the original is untouched, because strings are immutable).
Click Run to execute.
- Slice with
s[start:stop:step]; the stop index is excluded. - Omit start (s[:n]) or stop (s[n:]) to go from the beginning or to the end.
s[::-1]reverses a string; slicing returns a new string.
3String methods
Strings come with many built-in methods (functions called with a dot). The ones you must know:
| Method | Does |
|---|---|
upper() / lower() | Returns the string in upper / lower case |
strip() | Removes spaces from both ends |
split() | Splits into a list of words |
join() | Joins a list of strings into one |
replace(a, b) | Replaces every a with b |
find(x) | Returns the index of x (or -1 if absent) |
isalpha() / isdigit() | True if all letters / all digits |
You can also join strings with + (concatenation) and repeat with *: "ab" * 3 gives "ababab". Remember — every method returns a new string; the original never changes.
Click Run to execute.
- Key methods: upper/lower, strip, split, join, replace, find, isalpha/isdigit.
+concatenates strings;*repeats them.- Every string method returns a NEW string — the original is unchanged (immutability).
4Project: Password Strength Checker
Time to combine everything — strings, methods, loops and conditions — into a real, useful program. This Password Strength Checker scores a password on three rules: length, having a digit, and having a capital letter. Run it, then change the password and the rules:
Click Run to execute.
- Real programs combine strings, loops and conditions.
- Loop over each character and use isdigit()/isupper() to inspect it.
- Build a score with if-statements, then decide the result.
★ Project: extend the Password Strength Checker
Using the playground above, improve the checker:
- Add a fourth rule: the password must contain a lowercase letter (use islower()).
- Change the strength bands so a score of 4 prints 'Very Strong'.
- Test it with three different passwords and check the results make sense.
- Bonus: reject any password shorter than 6 characters with a clear message.
Ready for the chapter test?
Answer 5 questions. Score 60% or more to mark this chapter complete.
Start the test →💡 Log in to save your progress and earn the certificate.