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

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Hand Crafting your own program By Eric Davis for CS103.
An Introduction to Textual Programming
07/10/ Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.
Manipulating Text In today’s lesson we will look at: why we might want to pick out parts of text strings some BASIC functions that can be used to chop.
Lesson 6. Python 3.3 Objectives. In this lesson students will learn how to output data to the screen and request input from the user. Students will also.
Working on exercises (a few notes first)‏. Comments Sometimes you want to make a comment in the Python code, to remind you what’s going on. Python ignores.
Decision Structures, String Comparison, Nested Structures
Validation final steps Stopping gaps being entered in an input.
Homework #4: Operator Overloading and Strings By J. H. Wang Apr. 17, 2009.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
Exception Handling and String Manipulation. Exceptions An exception is an error that causes a program to halt while it’s running In other words, it something.
02/03/ Strings Left, Right and Trim. 202/03/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
13/06/ Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
Chapter Four Common facilities of procedural languages.
Introduction to Python Lesson 2a Print and Types.
Learning to use a ‘For Loop’ and a ‘Variable’. Learning Objective To use a ‘For’ loop to build shapes within your program Use a variable to detect input.
Input, Output and Variables GCSE Computer Science – Python.
Data Capture Forms What are they?. Example 1 Example 2.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
String Manipulation Part 1
Introduction to Programming
String Methods Programming Guides.
Databases.
Agenda Warmup AP Exam Review: Litvin A2
Introduction to Programming
CMPT 120 Topic: Python strings.
Validation Rules BCS-CA2-4 Students will use database software to create, edit & publish industry appropriate files.
Variables, Expressions, and IO
Open AvgLoop. It should provide SOME guidance for today’s assignment, but don’t just copy-paste….THINK.
Strings Part 1 Taken from notes by Dr. Neil Moore
Engineering Innovation Center
Strings in Python Creating a string.
String Manipulation.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Computer Science and an introduction to Pascal
Decision Structures, String Comparison, Nested Structures
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to Programming
Decision Structures, String Comparison, Nested Structures
Encryption and Decryption
4.1 Strings ASCII & Processing Strings with the Functions
CSC 221: Introduction to Programming Fall 2018
Coding Concepts (Data- Types)
Repetition In today’s lesson we will look at:
Introduction to Programming
Basic String Operations
Introduction In today’s lesson we will look at: why Python?
Variables In today’s lesson we will look at: what a variable is
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)
Introduction to Computer Science
User Input Keyboard input.
Introduction to Programming
Basic Lessons 5 & 6 Mr. Kalmes.
Python Strings.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Basic Mr. Husch.
ASCII LP1.
Starter Which of these inventions is: Used most by people in Britain
Python SAT 1 Feedback.
Validation Rules BCS-CA2-4 Students will use database software to create, edit & publish industry appropriate files.
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
ASCII and Unicode.
Getting Started in Python
Presentation transcript:

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

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!

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

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!”)

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)

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

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)

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

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.

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)