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.
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.
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.
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.
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".
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.
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.
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
- Find the original stringWhich variable or literal provides the text before the method call?
- Find the methodWhich operation appears after the dot, and does it have parentheses?
- Read any method argumentsFor a method such as replace() or split(), what tells Python what to find or use?
- Decide what the method returnsDoes it return a new string, or does split() return a list of strings?
- Find where the result is savedIs the result assigned to a new name, assigned back to the old name, printed, or ignored?
- 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 parentheses
text.lowerrefers to the method;text.lower()calls it and gets its result. - Expecting strip() to remove every space
strip()removes whitespace at the beginning and end, not spaces between words. - Treating split() results as one string
split()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 directly
join()expects string elements. A list such as[1, 2, 3]needs its values converted to strings before joining.
