PY-101 ยท Strings in depth
String methods
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก 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.
raw = " ASHA@Mail.COM "email = raw.strip().lower()print(email, "@" in email)csv_line = "tea,milk,bread"items = csv_line.split(",")print(items)print(" | ".join(items))print("Report.pdf".endswith(".pdf"), "2026".isdigit())
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
asha@mail.com True
['tea', 'milk', 'bread']
tea | milk | bread
True True ๐ฏ Turn the sentence into a hashtag: remove spaces at the ends, make every word start with a capital, join them with no spaces and put # in front. Print #LearnPythonToday.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
tag = "#" + "".join(w.capitalize() for w in sentence.split())
- 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.
- The Python idea
text.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"). - 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) Real-world scenario
Log analysis scripts split each line on spaces to pull out the time, the level and the message.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Formatting numbers and dates โ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
- String methods (docs.python.org)