Latest Awards
Complete 10 python programs which focus on loops.

morgan

Score over 100 points in total

vincebanter10

Complete any 5 python programs

vincebanter10

Complete 3 python list based tasks

vincebanter10

Complete 5 python list based tasks

vincebanter10

Complete 10 python list based tasks

dman12

Score over 1000 points in total

april-logan

Complete any pygame task

april-logan

Complete any 3 pygame tasks

april-logan

You have to complete 5 pygame tasks

april-logan

Score over 500 points in total

sam-edmund

Complete any pygame task

sam-edmund

Complete any 3 pygame tasks

sam-edmund

You have to complete 5 pygame tasks

sam-edmund

Complete any pygame task

12wrigleyf

Complete any 3 pygame tasks

12wrigleyf

You have to complete 5 pygame tasks

12wrigleyf

Score over 500 points in total

jhoughton12

Complete any 10 python prograns

jhoughton12

Complete 10 python programs which focus on loops.

jhoughton12

Python Score
tasks = 0
achivements = 0
freestyle = 0
Total = 0
Top 5 Scores
114gilo 3500
2joss-nolan-2 2530
3alexdawkins 2515
4byrnebrian 2080
512wattsl 1750
Click to view all scores
Who else completed this task?
No one has completed this task yet. Be the first!

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)


Leave a Reply

You must be logged in to post a comment.