Python Programming · Lesson 6 of 15 · Updated ·
Strings in Python — Indexing, Slicing and String Methods
A string is text: a sequence of characters inside quotes. Strings are immutable — you can't change a character in place; methods return a new string.
1. Indexing and slicing
s = "PYTHON"
print(s[0], s[-1]) # first and last
print(s[1:4]) # index 1, 2, 3
print(s[:2], s[2:]) # start to 2, 2 to end
print(s[::2]) # every second character
print(s[::-1]) # reversed
print(len(s))
P N
YTH
PY THON
PTO
NOHTYP
6
Slice rule: s[start:stop:step] — start included, stop excluded. Missing start = beginning; missing stop = end.
2. Useful string methods
text = " Learn Python at Skills101 "
clean = text.strip()
print(clean)
print(clean.lower())
print(clean.upper())
print(clean.replace("Python", "Java"))
print(clean.split())
print("-".join(["2026", "11", "08"]))
print(clean.startswith("Learn"), clean.endswith("101"))
print(clean.find("Python"), clean.count("n"))
Learn Python at Skills101
learn python at skills101
LEARN PYTHON AT SKILLS101
Learn Java at Skills101
['Learn', 'Python', 'at', 'Skills101']
2026-11-08
True True
6 2
| Method | Does |
|---|---|
strip() | remove spaces at both ends |
lower(), upper(), title() | change case |
replace(a, b) | replace all a with b |
split(sep) | break into a list (default: spaces) |
sep.join(list) | join a list into one string |
find(x) | index of x, or -1 if missing |
count(x) | how many times x appears |
isdigit(), isalpha(), isalnum() | check the kind of characters |
3. Checking and looping
pin = "302029"
print(pin.isdigit(), len(pin) == 6)
word = "programming"
vowels = 0
for ch in word:
if ch in "aeiou":
vowels += 1
print(vowels)
True True
3
4. Strings are immutable
name = "ravi"
# name[0] = "R" ← TypeError: strings can't be changed in place
name = "R" + name[1:] # build a new string instead
print(name)
Ravi
5. Interview programs
Palindrome check
def is_palindrome(s):
s = s.lower().replace(" ", "")
return s == s[::-1]
print(is_palindrome("Madam"), is_palindrome("Never odd or even"), is_palindrome("Skills"))
True True False
Count each character
from collections import Counter
print(Counter("banana").most_common())
[('a', 3), ('n', 2), ('b', 1)]
Reverse each word
sentence = "learn to code"
print(" ".join(word[::-1] for word in sentence.split()))
nrael ot edoc
Common mistakes
- Expecting
s.upper()to changes— it returns a new string; writes = s.upper(). - Off-by-one in slices (stop is excluded).
- Using
+in a loop to build big strings — collect parts in a list andjointhem.
Quick recap
- Index from 0; negative indexes count from the end.
s[start:stop:step];s[::-1]reverses.- Methods return new strings: strip, lower, upper, replace, split, join, find, count.
- Strings are immutable.