Presentation is loading. Please wait.

Presentation is loading. Please wait.

Manipulating Text In today’s lesson we will look at:

Similar presentations


Presentation on theme: "Manipulating Text In today’s lesson we will look at:"— Presentation transcript:

1 Manipulating Text In today’s lesson we will look at: why we might want to pick out parts of text strings some Python functions that can be used to chop up text

2 Why Process Text? There might be occasions when a particular piece of information can be presented in different ways. Think about a person’s name – you might want to display: the whole name – e.g. Andrew Virnuls just the forename – e.g. Andrew initial and surname – e.g. A Virnuls However, your user wouldn’t be very happy if you asked them for all the possible versions of their name!

3 Concatenation The simplest thing you can do with text strings is join them together – this is called concatenation We looked at this in lesson 2 – we can use the + operator to join words, e.g. forename = “Andrew” surname = “Virnuls” fullname = forename + “ “ + surname

4 String Length Sometimes you need to calculate the length of a string – e.g. to make sure that it will fit on the screen, or to check that it has the right number of characters (e.g. a bank sort code has 6 digits) There is a function called len() that tells you how long a string is, e.g. sortcode = input “Please enter your sort code: “ chars = len(sortcode) if chars <> 6: print(“That doesn’t look right!”)

5 Characters from the Start
Sometimes you might want to take part of the string from the left hand end. You can do this using the position of the character you want, e.g. forename = input(“What is your name? ”) initial = forename[0] Print(“Your first initial is ” + initial)

6 Characters from the End
If you use negative numbers, Python counts back from the end of the string. For example... forename = input(“What is your name? “) last = forename[-1:] print(“Your name ends with ” + last) i.e. you can omit the second number

7 Characters from the Middle
Should you want to take some characters from the middle of a string, you use two numbers The second number needs to be one more than the position of the last character you want fore = input(“What is your name? “) middle = fore[1:-1] print(“The middle of your name is ” + middle)

8 Upper and Lower Case People aren’t always very good at using capital letters – even for their own name You might also want to capitalise some text to use it as a title. Python has three methods to help: .upper() to convert text to upper case .lower() to convert text to lower case .title() to capitalise the first letter of each word

9 Finding a Character If you want to know if a string contains a certain character, you use the find() method You give it a string and a character, and it returns the position of the first occurrence of the character within the string, for example: string = "hello world" print(string.find(“e”)) If the character doesn’t appear in the string, then the answer is -1.

10 Putting It All Together
What code could you use to separate a full name into a forename and a surname? fullname = input("What is your full name? ") space = fullname.find(" ") forename = fullname[:space].title() surname = fullname[space+1:].title() print("Your forename is " + forename) print("Your surname is " + surname)


Download ppt "Manipulating Text In today’s lesson we will look at:"

Similar presentations


Ads by Google