Lesson 32 of 60
โฎPrev Nextโฏ

PY-101 ยท Strings in depth

String methods

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

32of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    String methods are built-in tools you call on any string with a dot, such as text.split() or name.startswith("Dr"). They clean, search and reshape text, and they always return a new string or value because strings never change. split() turns text into a list and join() turns a list back into text.

    • Split and join text
    • Search with find, startswith, count
    • Check content with isdigit and isalpha

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    String methods, running
    1. raw = " ASHA@Mail.COM "
    2. email = raw.strip().lower()
    3. print(email, "@" in email)
    4. csv_line = "tea,milk,bread"
    5. items = csv_line.split(",")
    6. print(items)
    7. print(" | ".join(items))
    8. print("Report.pdf".endswith(".pdf"), "2026".isdigit())
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemA sign-up form gets " ASHA@Mail.COM ". Before you save it you need it trimmed, lower-cased and checked that it contains an @. Three method calls do it.
    2. The Python ideatext.split(sep) sep.join(items)String methods are built-in tools you call on any string with a dot, such as text.split() or name.startswith("Dr").
    3. In real lifeLog analysis scripts split each line on spaces to pull out the time, the level and the message.

    Start with a question

    A sign-up form gets " ASHA@Mail.COM ". Before you save it you need it trimmed, lower-cased and checked that it contains an @. Three method calls do it.

    Most real data arrives as messy text: names, emails, CSV lines, chat messages. These methods are how you tidy it.

    Explain it like I'm 10

    String methods are a toolbox for words: scissors (split), glue (join), a magnifying glass (find) and an eraser (strip).

    The technical explanation

    text.split(",") cuts text at every comma into a list; ", ".join(items) glues a list back together.

    find(x) returns the position of x, or -1 if missing. startswith and endswith return True or False.

    isdigit() is True when every character is a digit, handy for checking a phone number or PIN.

    Syntax

    text.split(sep)    sep.join(items)
    text.find(x)    text.startswith(x)    text.count(x)

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Log analysis scripts split each line on spaces to pull out the time, the level and the message.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. "a-b-c".split("-")?
    2. 2. "-".join(["x", "y"])?
    3. 3. "hello".find("z")?
    4. 4. "12a".isdigit()?
    5. 5. "banana".count("a")?

    Questions people ask

    How do I split a string into words in Python?

    Use text.split() with no argument. It splits on any whitespace and ignores extra spaces.

    How do I join a list of numbers into a string?

    Convert them first: ", ".join(str(n) for n in nums). join only accepts strings.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner How do I split a string into words in Python?

    Use text.split() with no argument. It splits on any whitespace and ignores extra spaces.

    beginner How do I join a list of numbers into a string?

    Convert them first: ", ".join(str(n) for n in nums). join only accepts strings.

    intermediate "a-b-c".split("-")?

    ['a', 'b', 'c']. split cuts at every dash.

    intermediate "-".join(["x", "y"])?

    "x-y". join puts the separator between items.

    Quick revision

    Remember

    • Split and join text
    • Search with find, startswith, count
    • Check content with isdigit and isalpha
    • "a,b,,c".split(",") keeps empty pieces. " a b ".split() with no argument splits on any run of spaces and drops the empties.

    Syntax

    text.split(sep)    sep.join(items)
    text.find(x)    text.startswith(x)    text.count(x)

    Official documentation