+91 81900 98289

Modify Strings

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.

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)

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

Strip spaces

strip removes spaces at both ends.

print(" Chennai ".strip()) # spaces at both ends are removed

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
Calling strip without using the return value does not change raw.