This Modify Strings lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.
upper, lower, strip, title, and replace each return a new string. The original text stays as it was.
Chain methods when each step should feed the next. Assign the result if you need it later.
Example 1: Start here
Calling strip without using the return value does not change raw.
raw = " python " # text
clean = raw.strip().upper() # new text in capitals
print(clean)
print(raw)
Example 2: Change case
upper and lower return a new string. The original text is unchanged.
print("python".upper()) # a new string in capitals
print("PYTHON".lower()) # a new string in small letters
Example 3: Strip spaces
strip removes spaces at both ends.
print(" Chennai ".strip()) # spaces at both ends are removed
Example 4: Replace a word
replace returns a new string with the word swapped.
print("learn java".replace("java", "Python")) # a new string with the word swapped
