Previous Lecture Complete and continue  

  Positions and Accessing Characters

Positions in a String

Each character in a string has its own position. The first position in a string is always 0, and the last position is always one less than the string's length. The picture below shows the position of each character in the string 'string'.

Accessing a Character

You can access individual characters in a string using the character's position. To do this, we use bracket notation. Bracket notation uses square brackets ([ ]) to access a property. If you want to find the the first character of the string 'I like puppies', you would first type the string, then open up and close square brackets, and finally, inside the brackets, you type the position of the character you want to access, which would be 0. Check out the code snippet below to see how to use bracket notation to access the first character of the string 'I like puppies':

'I like puppies'[0];

The code snippet 'I like puppies'[0]; will return a string of 'I'.