Module 7 · Strings — topic 8 of 22
String methods
An overview of the str methods: how to call them, how to find them with dir() and help(), the main groups (case, search, test, clean, split and join) and chaining several calls.
Learning objectives
- Call string methods with dot notation
- Discover methods with dir() and help()
- Chain methods to transform text in steps
Definition
An overview of the str methods: how to call them, how to find them with dir() and help(), the main groups (case, search, test, clean, split and join) and chaining several calls.
Introduction
A method is a function that belongs to a value and is called with a dot: "hello".upper(). Strings come with more than forty methods for changing case, searching, testing, cleaning and splitting text. The following lessons look at the most important ones individually.
Why it matters
Methods turn complicated text tasks into one call. Instead of writing loops to remove spaces or count words, you call strip() or split(). Knowing what is available saves you from re-inventing it.
Detailed explanation
Syntax: value.method(arguments). The value before the dot is the string the method works on.
Because strings are immutable, transforming methods return a new string; testing methods return True or False; searching methods return positions or counts.
dir(str) lists all methods; help(str.strip) explains one. In VS Code, typing a string followed by a dot shows them all.
Methods can be chained left to right, because each returns a string: text.strip().lower().replace(" ", "-").
Long chains are hard to debug; break them into named steps when they grow beyond three or four calls.
Syntax
- The string comes before the dot.
- Some methods take arguments, others don’t.
- Chained calls run left to right.
Code examples
A few methods
Hello, Python hello, python 2 True
Discovering methods
True True False
Strings have no reverse() method; use slicing [::-1] instead.
More examples
Making a URL slug with a method chain
learn-python-the-basics loops-lists-and-more
Step-by-step explanation
- strip() removes outer spaces and lower() makes everything lower-case; the result is stored in cleaned.
- The loop removes each punctuation character with replace(), rebinding cleaned each time.
- split() with no argument splits on any run of spaces, so double spaces don’t create empty words.
- "-".join(...) glues the words with hyphens to form a URL-friendly slug.
Expected output
Running “Making a URL slug with a method chain” prints exactly:
Practical applications
- Blogs and stores generate URL slugs from titles.
- Search features normalise text with lower() and strip() before comparing.
- Validation uses isdigit() and similar methods to check form fields.
Best practices
- Learn the method groups rather than memorising every method.
- Use dir() and help() or editor completion to explore.
- Break long chains into named steps.
Common mistakes
Calling a list method on a string
Avoid
Prefer
Strings have no reverse() method; check dir(str) when unsure.
Forgetting parentheses
Avoid
Prefer
Without parentheses you refer to the method itself instead of calling it.
Key takeaways
- Methods are called with value.method(args).
- Transforming methods return new strings; is-methods return Booleans.
- Methods can be chained because each returns a string.
Try it yourself
- 1Use help(str.zfill) in the shell and then pad the number 42 to five digits.
- 2Write a chain that turns " HELLO world " into "Hello World".
Finished reading? Mark it complete to update your dashboard.
Suggested practice
17 questions cover the “Strings” module. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is printed?