Skip to content

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.

Beginner 12 min read·Lesson 89 of 306 in Python Programming

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.

GroupExamples
Caseupper(), lower(), title(), capitalize(), swapcase()
Cleanstrip(), lstrip(), rstrip(), replace()
Searchfind(), index(), count(), startswith(), endswith()
Testisdigit(), isalpha(), isalnum(), isspace(), isupper()
Split/joinsplit(), splitlines(), join(), partition()
Aligncenter(), ljust(), rjust(), zfill()

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

Python
result = text.method(arguments)
result = text.strip().lower()
  • The string comes before the dot.
  • Some methods take arguments, others don’t.
  • Chained calls run left to right.

Code examples

A few methods

Python
text = " Hello, Python "
print(text.strip())
print(text.lower())
print(text.count("o"))
print("2026".isdigit())
Output
Hello, Python
  hello, python  
2
True

Discovering methods

Python
names = [m for m in dir(str) if not m.startswith("_")]
print(len(names) > 40)
print("zfill" in names, "reverse" in names)
Output
True
True False

Strings have no reverse() method; use slicing [::-1] instead.

More examples

Making a URL slug with a method chain

Python
def slugify(title):
cleaned = title.strip().lower()
for ch in "!?,.:":
cleaned = cleaned.replace(ch, "")
return "-".join(cleaned.split())
for t in [" Learn Python: The Basics! ", "Loops, Lists and More"]:
print(slugify(t))
Output
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:

TextOutput
learn-python-the-basics
loops-lists-and-more

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

"abc".reverse()

Prefer

"abc"[::-1]

Strings have no reverse() method; check dir(str) when unsure.

Forgetting parentheses

Avoid

name.upper

Prefer

name.upper()

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

  1. 1
    Use help(str.zfill) in the shell and then pad the number 42 to five digits.
  2. 2
    Write 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?
Practise this module