✦ Python Foundations Working with Data module

Concept 19 · String Methods

Clean up text.
Keep the original safe.

String methods help a program clean, reshape, and organize text while keeping the original string value safe until you choose to save a new result.

01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

Ask text for a useful new result.

A string method is an operation you ask a string value to perform. Read text.method() as “use this text with this method.” Many methods return a new result, so connect this lesson to return values: original string → string method → new result.

message = "  Hello Python  "
cleaned = message.strip()

print(cleaned)
print(message)

message contains the original string. .strip() asks that string for a version without whitespace at its beginning and end. The method returns that new string, and cleaned stores the result. message still refers to its original value.

hello python ['Ana', 'Marko', 'Jelena']

Compare message.strip() with message = message.strip(). The first call produces a result that is not saved. In the second, the name message is assigned the new string value. The original string was not changed; the name now refers to a different result.

↳

Read every method call as a value flow: message → strip() → new string → cleaned. A method can return a result just as the functions from Concept 17 returned a result.

strip() cleans only the ends
name = "  Ana Marko  "
cleaned_name = name.strip()
print(cleaned_name)

cleaned_name becomes "Ana Marko". The space between Ana and Marko stays there: strip() removes whitespace from the beginning and end, not every space inside the string. This is useful when cleaning user input before using it further.

lower() and upper() normalize letter case
answer = " YES "
cleaned_answer = answer.strip().lower()
loud_answer = cleaned_answer.upper()

print(cleaned_answer)
print(loud_answer)

lower() returns a version with lowercase letters, and upper() returns one with uppercase letters. Here the cleaned answer is "yes". Normalizing user input makes simple comparisons more reliable when people type different capitalization.

A simple method chain is several result steps
normalized = answer.strip().lower()

Read this chain in order: strip() returns a cleaned string, then lower() is called on that returned result and returns a normalized string. Think: original → strip() → cleaned string → lower() → normalized string.

replace() returns updated text
text = "I like Java"
updated = text.replace("Java", "Python")
print(updated)
print(text)

The first argument tells replace() what text to find; the second tells it what to use instead. updated receives "I like Python", while text still contains "I like Java".

split() turns one string into a list of strings
names = "Ana,Marko,Jelena"
parts = names.split(",")
print(parts)

split() starts with a string but returns a list of strings: ["Ana", "Marko", "Jelena"]. The separator argument tells Python where to divide the text. This connects one text value to the lists you already know.

join() turns a list of strings into one string
words = ["Python", "AI", "Tutor"]
result = " ".join(words)
print(result)

Read " ".join(words) as “take the strings in words and join them with a space between them.” The result is "Python AI Tutor". split() and join() often move in opposite directions: text → list, then list → text, without being perfect opposites in every situation.

A small text-cleaning flow
raw_tags = "python,django,ai"
cleaned = raw_tags.strip().lower()
tags = cleaned.split(",")
result = " | ".join(tags)

print(result)

This small flow keeps each result visible: clean the text, split it into a list, then join the list with a different separator. The input has no spaces after commas, so this example stays focused on the methods rather than on extra list-cleaning steps.

A practical string-method reading checklist

  1. Find the original stringWhich variable or literal provides the text before the method call?
  2. Find the methodWhich operation appears after the dot, and does it have parentheses?
  3. Read any method argumentsFor a method such as replace() or split(), what tells Python what to find or use?
  4. Decide what the method returnsDoes it return a new string, or does split() return a list of strings?
  5. Find where the result is savedIs the result assigned to a new name, assigned back to the old name, printed, or ignored?
  6. Check which value is used laterDoes later code use the original string or the new result?

Common beginner mistakes

  • Expecting a method to automatically change the original stringMost string methods return a new result. Save that result when later code needs it.
  • Forgetting the parenthesestext.lower refers to the method; text.lower() calls it and gets its result.
  • Expecting strip() to remove every spacestrip() removes whitespace at the beginning and end, not spaces between words.
  • Treating split() results as one stringsplit() returns a list of strings, so read its result with the list ideas you already know.
  • Reading join() backwardsThe separator string comes before .join(): "-".join(parts) puts that separator between the strings in the list.
  • Trying to join numbers directlyjoin() expects string elements. A list such as [1, 2, 3] needs its values converted to strings before joining.

Python Playground

Loading Python runtime…

Change the text, try another method order, and compare the original value with each new result. Then change the separator used by split() or join() and observe the result type and text.

string_methods.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Read each data operation before choosing an answer.

message = "  PyThOn tutor  "
cleaned = message.strip().lower().replace(" ", "-")
print(cleaned)
print(message)
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.