String methods
There are a number of string methods which you can use for all of your string tomfoolery! We are not yet at the stage where we can explain what a method is, but for now just think of it as something a string can do. You can find more about string functions at the official documentation for python.
text = "apple pear apple pear"
print text.count("pear")
print text.capitalize()
print text.center(70)
print text.replace("apple", "pear")
Notice that in order to use the methods above you have to put a full stop in followed by the name of the method. Depending on the method it will either have empty brackets after it or some values. These are known as parameters.
text.count("pear") – This will count the number of times pear appears in the text string. You can replace pear with any other string (even a variable!).
text.capitalize() – This will capitalize the string. A bit like a virtual teacher! Notice it does not take any parameters but even so we have to put the empty brackets in (THIS IS IMPORTANT!)
text.center(70) – This will try and centre the text assuming it has 70 characters to play with. A quick and easy way to create a title for your command prompt programs!
text.replace("apple", "pear") – A very useful method! It will search for the first parameter and replace it with the second. It will do this every time it finds the first string.
change the above code so that text is now –
text = "pear apple banana kiwi apple mango"
Output: (clear)











