Presentation is loading. Please wait.

Presentation is loading. Please wait.

More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:

Similar presentations


Presentation on theme: "More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:"— Presentation transcript:

1 More about Strings

2 String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:  This has now become quite long and difficult to read  The money amount is not formatted as £10.50 as we would want either… 2 of 27

3 String Formatting  We can improve this by using the string format function:  The numbers in {}’s mark slots in the printed string. They are formatted like this: { : }  The index numbers start at zero (0)  Notice the dot after the closing quote (it can start a new line if necessary) 3 of 27 Yes, I know!

4 String Formatting There are many format specifiers; here are a couple:  Using a comma as a thousands separator  Applying type-specific formatting formats the fourth value as a floating point number to 2 decimal places. 4 of 27

5 More about print( )  By default, the print() function adds a space between each comma-separated part a new line after each print  You can change this default behaviour: 5 of 27

6 Strings as Sequences  What is a sequence? a set of related items that follow each other in a particular order (Oxford English Dictionary)  Strings in Python are sequences of characters  You can refer to a character by using its index Prints: Notice that indexes start at 0 6 of 27

7 Printing All Characters  How could you print all the characters in phrase?  You wouldn’t want to do it like this…  What do we need if we want to repeat something?  A loop… we need to iterate over the string. How can do this? 7 of 27

8 Printing All Characters  What if we used a variable for the index?  Does this help you to construct your loop?  What is being repeated, what is the condition, is there any set up?Try this… Set up Repeated What is the condition for repeating?

9 Printing All Characters: A Loop  What is being repeated?  What is the condition? the index must be less than the length of the string  What is the set up? 9 of 27

10 Your Turn 1. Write a program to print an input phrase backwards. E.g. if the input phrase is “Hello”, your program should print “olleH” 2. Write a program to count all the instances of the letter “e” in an input phrase Ask the user to enter a phrase Iterate over the string, counting e’s as you go Print this value 10 of 27

11 Solutions 1. 2.

12 Strings as Sequences  There are special constructs in Python specifically for sequences  The first we will look at is for, which is another loop  The for loop, iterates over a sequence, automatically assigning each element in turn to the variable; letter in this case.  Notice the use of the keyword, in 12 of 27 This is different from the for loop used in most languages…

13 While and For… These two programs print the same thing…  Using a for loop:  Using a while loop: 13 of 27

14 The for Loop  This is the structure of a for loop: for in :  Remember that strings are sequences… An element of a string is a character  Each time around the loop: The automatically gets the next element in the sequence A hidden index moves along by one The loop stops automatically at the end of the sequence 14 of 27

15 Your Turn Write the pseudo code for this (using the while structure) Then write the code using a) A while loop b) A for loop Write a program to count all the instances of the the letters “t” and the letter “e” in an input phrase Ask the user to enter a phrase Find out how many t’s are in the phrase Find out how many e’s are in the phrase Print this value 15 of 27

16 Pseudo Code Start get phrase index = 0, t_count = 0, e_count = 0 while index < length of phrase get char from phrase at index position if char == “t” t_count = t_count + 1 else if char == “e” e_count = e_count + 1 end-if index = index + 1 end-while display t_count, e_count Stop

17 Solution 17 of 27

18 Solution 18 of 27

19 Strings are Immutable What does immutable mean? unchanging over time or unable to be changed (Oxford English Dictionary)  When you do this, you are actually creating a new string  This may sound surprising but try this: 19 of 27

20 Copying a String, missing a letter  Start by copying the string character by character:  Now let’s ignore any g’s in the phrase 20 of 27

21 The String Module  There are some useful constants in the string module  Notice that the whitespace characters are not printable… 21 of 27

22 Strings as Sequences: Using in  The keyword in scans a sequence, looking for an element. It returns true if it finds it, false otherwise.  The element can be any size e.g.  You can use this in a loop… What does this do? 22 of 27

23 Your Turn 1. Write a program that: Asks the user to enter a phrase Counts the number of uppercase characters found in the phrase Prints this value 2. Challenge Task Ask the user to enter a phrase Count the whitespace characters Create new phrase which:  Has a # in place of any digits  Does not include any of the whitespace characters 23 of 27

24 Solutions Count uppercase characters 24 of 27

25 Solutions Change the phrase and count whitespace…

26 String Slicing  As well as getting single items from a sequence, you can get segments. This is called string slicing. [start_inc:end_ex]  Try these…  What are they each doing? 26 of 27

27 Summary We have looked at  string formatting using the format() function  how you can change the default behaviour of the print function  strings as sequences of characters  how you can iterate over a sequence using while and for  how to use the in keyword  string slicing  Complete MoreStringsExercises.pdf for homework 27 of 27


Download ppt "More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:"

Similar presentations


Ads by Google