Pylerno
English

Text methods

Normalize text intentionally while preserving its meaning.

On this page

The concept

Text received from a user often contains leading or trailing whitespace. strip() removes whitespace at the edges; it does not compress spaces in the middle. split() without an argument separates whitespace-delimited words. A separator string's join() method combines strings from an iterable.

String methods create new strings because strings are immutable. Assign the returned result when you want to keep it. in answers whether a substring exists. find() returns the first position or -1; startswith and endswith express boundary checks directly. Choose the smallest operation that clearly describes your intent.

Worked example

python
raw = "  Python Is Fun  "
clean = raw.strip()
print(clean.lower(), clean.upper())
print(clean.replace("Fun", "Useful"))
words = clean.split()
print(words)
print("-".join(words))
print("Python" in clean)
print(clean.startswith("Python"), clean.endswith("Fun"))
print(clean.find("Is"), clean.find("Java"))
print("banana".count("a"))

Trace the result

  1. Compare raw and clean: strip removes the surrounding spaces but leaves the original raw value unchanged.
  2. split produces a list, while "-".join(words) produces a new string from that list.
  3. find("Java") returns -1, which means no match. The count call answers a different question: how many occurrences exist.
text
python is fun PYTHON IS FUN
Python Is Useful
['Python', 'Is', 'Fun']
Python-Is-Fun
True
True True
7 -1
3
Line-by-line walkthrough

Line 1: raw = " Python Is Fun "

Evaluate ' Python Is Fun ', then bind or update raw with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 2: clean = raw.strip()

Evaluate raw.strip(), then bind or update clean with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.

Line 3: print(clean.lower(), clean.upper())

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 4: print(clean.replace("Fun", "Useful"))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 5: words = clean.split()

Evaluate clean.split(), then bind or update words with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.

Line 6: print(words)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 7: print("-".join(words))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 8: print("Python" in clean)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 9: print(clean.startswith("Python"), clean.endswith("Fun"))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 10: print(clean.find("Is"), clean.find("Java"))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 11: print("banana".count("a"))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

Writing raw.strip without parentheses retrieves the method instead of calling it. Also, over-aggressive cleaning can alter meaningful data.

Check your understanding

What does this program print?

python
print("  red tea  ".strip())
Choose one answer

Search documentation

Share this page