What is a method?#
A method is a function that belongs to an object. You call it with a dot: text.upper(). Python strings come with dozens of built-in methods.
Strings are immutable, so string methods never change the original string - they return a new one. Always store the result:
name = "ravi"
name.upper() # result thrown away
print(name) # ravi
name = name.upper() # store it
print(name) # RAVI
Changing case#
| Method | Result for "hello World" |
|---|---|
upper() |
HELLO WORLD |
lower() |
hello world |
title() |
Hello World |
capitalize() |
Hello world |
swapcase() |
HELLO wORLD |
Use .lower() before comparing user input: if answer.lower() == "yes": accepts "Yes", "YES" and "yes".
Removing spaces: strip, lstrip, rstrip#
Users often type extra spaces. These methods remove whitespace (spaces, tabs, newlines) from the ends:
Splitting and joining#
split() - string → list#
join() - list → string#
separator.join(list_of_strings):
split() एक string को टुकड़ों में तोड़कर list बनाता है।
"Ravi,21,Indore".split(",") → ['Ravi', '21', 'Indore']
join() इसका उल्टा है - list के टुकड़ों को जोड़कर string बनाता है। इसमें जोड़ने वाला निशान (separator) पहले लिखा जाता है:
"-".join(['25', '11', '2026']) → "25-11-2026"
याद रखने का तरीका: "separator.join(list)" - यानी "इस निशान से इन सबको जोड़ो"।
join() works only on strings. ", ".join([1, 2, 3]) raises TypeError. Convert first: ", ".join(str(n) for n in [1, 2, 3]) (or use map(str, ...)).
Searching#
| Method | Purpose | "banana" example |
|---|---|---|
find(x) |
index of first x, or -1 |
find("an") → 1 |
rfind(x) |
index of last x, or -1 |
rfind("an") → 3 |
index(x) |
like find, but ValueError if missing | index("z") → error |
count(x) |
how many times x appears |
count("a") → 3 |
startswith(x) |
begins with x? |
startswith("ba") → True |
endswith(x) |
ends with x? |
endswith(".pdf") → False |
Replacing#
Checking the content (is... methods)#
These return True or False:
| Method | True when... | Example |
|---|---|---|
isdigit() |
all characters are digits | "2026".isdigit() |
isalpha() |
all are letters | "Ravi".isalpha() |
isalnum() |
letters or digits only | "Room42".isalnum() |
isspace() |
only whitespace | " ".isspace() |
isupper() / islower() |
all letters upper / lower case | "ABC".isupper() |
Alignment and padding#
Putting it together: a password validator#
(any() returns True if at least one item is True - you'll see this pattern again with comprehensions.)
Quick reference#
| Task | Code |
|---|---|
| Remove spaces at ends | s.strip() |
| Lower case | s.lower() |
| Words list | s.split() |
| List → text | " ".join(words) |
| Replace text | s.replace(old, new) |
| Position of text | s.find(x) |
| Count text | s.count(x) |
| Is it a number? | s.isdigit() |
Summary#
- String methods return new strings - store the result.
- Clean input with
strip()andlower(). split()turns text into a list;sep.join(list)turns a list into text.- Search with
find,count,startswith,endswith; edit withreplace. - Validate with
isdigit,isalpha,isalnum.