Watch the video and then scroll down to see the sample code.
PHP sample code:
echo "<h3>This is a heading using the HTML h3 heading tag</h3>";
echo "This string is joined to " , "this other string.";
echo "</br>This string is on a new line.";
This video tutorial explains how to concatenate (join) strings, convert between integers and strings, change text to uppercase and lowercase, and also introduces indexing and arrays.
The sample code snippet below shows how to join (concatenate) strings, find the length of a string (the number of characters in a string), convert from integer to string, display specific characters in a string, split a string up and place each word in a separate array element, and convert string to uppercase and lowercase.
Don’t try all of this in one go. Have a go at one string operation at a time and only move on to the next one when you feel comfortable to.
var firstName = "Nicolas";
var lastName = "Cage";
// Concatenation means joining (eg. joining two strings together to form one string
// Below, is an example of a concatenated string being held in a variable
var joinednames = firstName + lastName;
// Below, is an example of concatention with spaces added between words.
document.write(firstName,' ',lastName,' ',"says \"hello\".");
var fullName = firstName + ' ' + lastName; // this combines two variables and a space between the strings into a new variable
var nameLength = fullName.length; // this calculates the length of the string as an integer
var age = 20;
document.write(fullName + age); // this prints the name string and then the variable (no spaces)
var ageString = age.toString(); // this converts the age integer variable to a string variable
/* To convert a string to an integer, the variable must firstly contain ony numbers. It can simply be multiplied by 1 to convert to an integer
eg. newIntVariable = stringVariable * 1
or you can use parseInt and parseFloat functions eg parseInt('77');
var message = 'Hello world!';
document.write(message.indexOf('w')) // the indexOf function finds the index number of a character in a string
document.write(message.charAt(8)) // this gives the character at index number 8
document.write(message.length) // this will give the length of the string
var messageArray = new Array();
messageArray = message.split(' '); //this splits the words (where there is a space) into a new array
document.write(messageArray,"</br>"); // this would return "Hello" (and a new line)
document.write(messageArray); // this would return "world!"
document.write(message.substring(4,8)); //this will show anything between index number 4 and 8
document.write(message.substr(4,8)); //substr is different to substring. this will show the 8 characters after index number 4.
document.write(message.toUpperCase()); // converts string to uppercase
document.write(message.toLowerCase()); // converts string to lowercase
document.write("<b>",message.toUpperCase(),"</b>"); // this prints the uppercase string in bold using the HTML bold element <b>
document.write("<h1>This is a heading</h1>"); // HTML elements can be put straight into a string
Next tutorial: Events
This tutorial explains how to use variables in Python to store integers and strings. It also explains how to concatenate (join) strings in Python.
In programming, a variable is used to store data in a program. A variable can only store one value at a time, for example some text, a number, or a true/false value.
The different data types that a variable can store include:
- string – this is text which can include letters, numbers, and other characters. String values are stored inside quotation marks
- integer – whole numbers
- float – numbers with a decimal point
- boolean – a true/false value
You can view the video below or click here to watch it on YouTube.