This resource is designed as a quick reference or revision guide. It has not been endorsed by any exam boards. If you spot any mistakes, please let me know and I'll fix them asap.
Any text in a program is stored as a string data type. Strings are made up of characters, which are individual letters, numbers or symbols.
Think of a piece of string hanging like a washing line outside. Pegged to that piece of string is each character in order.
Manipulating strings means changing or working with the characters inside a string.
The string "there" concatenated to the string "hello " becomes the string "hello there"
"it" is a substring of "biscuit": it's the 6th and 7th characters (counting from 1).
"it" is a substring of "biscuit": it's the 5th and 6th characters (counting from 0)
A string is some text such as "Hello" or "Area 51"
Variables let you store data.
String variables let you store text.
In this example a variable called name is set to the string value of "Bob"
A string is some text such as "Hello" or "Area 51"
Constants store a value that is set once and then never changes.
String constants let you give some text data a name so that you can use it to make your code easier to read or set options for your code that the user wont be able to change.
In this example, a constant called ERROR_MESSAGE is set to the string value of "Don't panic!"
A string is some text such as "Hello"
Concatenating means joining together or adding / appending on to the end.
Concatenating strings means joining two strings together.
In this example, two strings (first_name and last_name) are concatenated (joined together) and saved in another string variable called full_name with a space in between them.
A string is some text such as "Hello"
The length of a string means how many characters (including spaces, numbers and punctuation) there are in that string
This example createa a string called name and sets it to "Bob". It counts how many characters are in that string and stores the answer (3) into a variable called l
A string is some text such as "Hello"
A character is a single letter or digit.
Sometimes it's useful to get the 1st, 2nd or whichever character from a string.
This example gets the first character of a string called name and stores it into a variable called first_letter
Variables let your program store data of a particular data type.
Integer variables store whole numbers. String variables store text.
Sometimes you need to combine text with integer variables and string variables when you display it to the screen.
This example has a string variable called name (set to "Bob") and an integer variable called score (set to 10). It combines both variables with some text to display the message "Bob has 10 points"
Often it's useful to convert a number (integer or real data type) to text (string data type) so that it can be processed or sent as an output later in the program.
This example converts the integer number 1000 to a string containing "1000"
Often it's useful to round a decimal number (real data type) to a whole number (integer data type).
It can also be useful to convert a string representation of a number (e.g. one typed in by the user) to an integer
This example converts the string "1000" to the integer 1000
Often it's useful to convert a string representation of a number (e.g. one typed in by the user) to a decimal number for use in calculations (real data type)
This example converts the string "1.1234" to the real value 1.1234
It can be really useful to split one string into lots of smaller parts whenever you see a particular character.
This example will split the string "red,green,blue" into an array of smaller strings: "red", "green" and "blue"
It will then display the first colour: red
A string is some text. A substring is part of a bigger string. For example, "name" is a substring of "Hello, what's your name?".
This example creates a variable containing the string "What is your name" then displays the substring of the 5th and 6th characters ("is")
Loading...