Learners will be able to:
- Know what a statement, strings and inputs are
- Code to enter data (strings) into the program
- Apply these skills to develop a simple James Bond solution.
In this lesson you will learn about strings and how to use them in Python. You can enter them and manipulate them and also change and replace them and a whole load of other tricks.
Part 1: Print, your first program:
Part 1: Print, your first program:
- Run the code below, what does it do?
- The print is know as a 'statement' and prints what every is between the parenthesis.
- Make a code to print out your name, age and favorite food.
Strings:
Every letter, digit, and punctuation mark is a character. There are also many characters that are invisible on screen, such as the space, tab, and carriage-return characters. Most programming languages provide a data type called 'character' or 'char', each value represents a single character from a predefined set, such as ASCII or Unicode. Each character has its own binary pattern. Most programming languages have a data type called a string, which is used for data values that are made up of ordered sequences of characters, such as "hello world". A string can contain any sequence of characters, visible or invisible, and characters may be repeated. The number of characters in the string is called its length, and "hello world" has length 11 - made up of 10 letters and 1 space. There is usually a restriction on the maximum length of a string. There is also such a thing as an empty string, which contains no characters - length 0. A string can be a constant or variable. If it is a constant, it is usually written as a sequence of characters enclosed in single or double quotation marks, ie 'hello' or "hello"
Part 2: raw_input:
Every letter, digit, and punctuation mark is a character. There are also many characters that are invisible on screen, such as the space, tab, and carriage-return characters. Most programming languages provide a data type called 'character' or 'char', each value represents a single character from a predefined set, such as ASCII or Unicode. Each character has its own binary pattern. Most programming languages have a data type called a string, which is used for data values that are made up of ordered sequences of characters, such as "hello world". A string can contain any sequence of characters, visible or invisible, and characters may be repeated. The number of characters in the string is called its length, and "hello world" has length 11 - made up of 10 letters and 1 space. There is usually a restriction on the maximum length of a string. There is also such a thing as an empty string, which contains no characters - length 0. A string can be a constant or variable. If it is a constant, it is usually written as a sequence of characters enclosed in single or double quotation marks, ie 'hello' or "hello"
Part 2: raw_input:
- Sometimes you want the user to enter or input data in and can use the 'raw_input' code to enable this, run the program below.
- Adapt the code so that it prints out your name
- Can you alter the code so that the program says Hello to you?
- Create program which asks the user questions
Part 3: Strings are not numbers they do not have values:
- A string is a technically a code stored in memory that represents a symbol or a letter or number. It does not have a value.
- Don't believe me?, run the code below, what happens, why?
- So how do you get it to work properly?
- What about if you want to get the user to add a number of their choice, can you use raw_input?
- Add another code line to allow the user to add another number like line 6
- If raw_input does not work what can you use? (int, input, you may need to use IDLE)
Part 4: Strings can do this:
- You can create a number of variables at once with either string values or values, run the code below for an example.
- Can you create a program with 3 or even 4 variables?
- Change the operator (+, -, /, *) to make a new sum.
- Remove the ### on line 9 and run, what happens?
- The % can be used in place of the variables
- You can use variables instead but it is very chewy, can create the same line as line 9 but use variables instead, (clue..."dfdf", + "sdjhs")
Mr Bond!:
|
|
Try some of these string codes:
|
Homework
|
|
|