← Back to Chapters

Python String Methods

? Python String Methods

⚡ Quick Overview

Python provides many built-in string methods to manipulate and analyze text data. Since strings are immutable, these methods always return a new string (or value) instead of changing the original.

Common things you can do with string methods:

  • Change case (uppercase, lowercase, title case, etc.).
  • Search inside strings and count occurrences of substrings.
  • Trim extra spaces and replace parts of a string.
  • Split a string into a list and join a list back into a string.

? Key Concepts

  • Syntax: String methods are called using variable.method(arguments), for example: text.upper().
  • Immutability: Methods never change the original string; they return a new one.
  • Case methods: upper(), lower(), title(), capitalize().
  • Search & count: count(), find(), startswith(), endswith().
  • Whitespace & replace: strip(), lstrip(), rstrip(), replace().
  • Split & join: split() turns a string into a list; join() does the opposite.

? Syntax and Basic Usage

The general syntax for calling a string method is:

result = string_value.method_name(optional_arguments)

For example:

message = "hello world"
upper_msg = message.upper()   # returns "HELLO WORLD"

You can also chain string methods:

clean_name = "  alice ".strip().title()  # "Alice"

? Code Examples

✏️ Changing Case
text = "hello world"
print(text.upper())       # HELLO WORLD
print(text.lower())       # hello world
print(text.title())       # Hello World
print(text.capitalize())  # Hello world
? Searching and Counting
text = "hello world"
print(text.count("l"))          # 3
print(text.find("world"))       # 6
print(text.startswith("hello")) # True
print(text.endswith("world"))   # True
✂️ Trimming and Replacing
text = "  hello world  "
print(text.strip())                    # "hello world"
print(text.lstrip())                   # "hello world  "
print(text.rstrip())                   # "  hello world"
print(text.replace("world", "Python")) # "  hello Python  "
? Splitting and Joining
text = "apple,banana,cherry"
fruits = text.split(",")
print(fruits)  # ['apple', 'banana', 'cherry']

joined = "-".join(fruits)
print(joined)  # apple-banana-cherry

? Live Output / Explanation

? What You Will See

  • Changing case:
    • upper() prints HELLO WORLD (all letters uppercase).
    • lower() prints hello world (all letters lowercase).
    • title() prints Hello World (first letter of each word uppercase).
    • capitalize() prints Hello world (only the first character of the whole string uppercase).
  • Searching and counting:
    • count("l") returns 3 because there are three l characters.
    • find("world") returns 6 which is the starting index of "world".
    • startswith("hello") returns True because the string begins with "hello".
    • endswith("world") returns True because the string ends with "world".
  • Trimming and replacing:
    • strip() removes spaces from both ends.
    • lstrip() removes spaces from the left side only.
    • rstrip() removes spaces from the right side only.
    • replace("world", "Python") changes the word world to Python.
  • Splitting and joining:
    • split(",") breaks the string into a list of substrings: ['apple', 'banana', 'cherry'].
    • "-".join(fruits) joins the list back into a single string apple-banana-cherry with - between words.

? Tips & Best Practices

  • Remember that strings are immutable; string methods do not modify the original string.
  • Always store the result of a method call in a new variable if you need to use the modified value.
  • Combine methods (chaining) like text.strip().upper() for powerful one-line transformations.
  • Use startswith() and endswith() for clean and readable prefix/suffix checks.
  • When using join(), remember it is called on the separator string, not on the list: "-".join(list_of_strings).

? Try It Yourself

  • Create a string and print it in uppercase, lowercase, and title case using the appropriate methods.
  • Count how many times the letter "a" appears in a long sentence using count().
  • Use replace() to change a name in a sentence (e.g., replace "world" with "Python").
  • Take a comma-separated string (e.g., "red,green,blue"), split it into a list, then join it back using " | " as a separator.
  • Experiment with strip(), lstrip(), and rstrip() on strings that have spaces or tabs at the edges.