Python gives every string a toolbox of built-in methods.
You call a method with dot notation, for example my_text.upper().
Below are a few handy ones to get you started. For the complete reference, see the official list on W3Schools (or similar sites on the web): https://www.w3schools.com/python/python_ref_string.asp
sample = " Python programming "
print(sample.strip()) # remove leading and trailing spaces
print(sample.lower()) # all lowercase
print(sample.upper()) # ALL UPPERCASE
print(sample.count("m")) # how many times 'm' appears
print(sample.replace("Python", "Java")) # swap words
print(sample.startswith(" P")) # True or False
print("gram" in sample) # membership test (True)Each method returns a new string (or a number/boolean) - the original stays unchanged unless you reassign it.
-
Create a file called
Task_6.py. -
Add a variable
quoteand set it to a short sentence of your choice. -
Use at least four different string methods on
quote, printing the result of each. Suggestions:title()find()replace()endswith()encode()
-
After each call, print the original
quoteagain to show it has not changed. -
Finally, print a one-line comment (using
#) summarizing which method you found most useful.
Run the file and compare your output with the documentation page to see what each method does.