PHP, Java, C++, C#, Visual Basic, and Python allow you to retrieve the individual characters of a string using substring notation. You can use index 0 to access the first character, index 1 to access the second character, and so on. The index of the last character is 1 less than the length of the string. The following program shows an example.

PHP

<?php
  $a = "Hello World";
  
  echo $a[0]; //it displays the first letter
  echo $a[6]; //it displays the letter W
  echo $a[10]; //it displays the last letter
?>

Notice: Please note that the space between the words “Hello” and “World” is considered a character as well. So, the letter W exists in position 6 and not in position 5.

Java

public static void main(String[] args) throws java.io.IOException {
  String a;

  a = "Hello World";
  
  System.out.println(a.charAt(0)); //it displays the first letter
  System.out.println(a.charAt(6)); //it displays the letter W
  System.out.println(a.charAt(10)); //it displays the last letter
}

Notice: Please note that the space between the words “Hello” and “World” is considered a character as well. So, the letter W exists in position 6 and not in position 5.

C++

#include <iostream>
using namespace std;
int main() {
  string a;

  a = "Hello World";

  cout << a.at(0) << endl;     //it displays the first letter
  cout << a.at(6) << endl;     //it displays the letter W
  cout << a.at(10) << endl;    //it displays the last letter
  return 0;
}

Notice: Please note that the space between the words “Hello” and “World” is considered a character as well. So, the letter W exists in position 6 and not in position 5.

C#

static void Main() {
  string a;

  a = "Hello World";

  Console.WriteLine(a[0]);     //it displays the first letter
  Console.WriteLine(a[6]);     //it displays the letter W
  Console.WriteLine(a[10]);    //it displays the last letter

  Console.ReadKey();
}

Notice: Please note that the space between the words “Hello” and “World” is considered a character as well. So, the letter W exists in position 6 and not in position 5.

Visual Basic

Sub Main()
  Dim a As String

  a = "Hello World"

  Console.WriteLine(a(0)) 'it displays the first letter
  Console.WriteLine(a(6)) 'it displays the letter W
  Console.WriteLine(a(10)) 'it displays the last letter

  Console.ReadKey()
End Sub

Notice: Please note that the space between the words “Hello” and “World” is considered a character as well. So, the letter W exists in position 6 and not in position 5.

Python

a = "Hello World"

print(a[0])         #it displays the first letter
print(a[6])         #it displays the letter W
print(a[10])        #it displays the last letter

Notice: Please note that the space between the words “Hello” and “World” is considered a character as well. So, the letter W exists in position 6 and not in position 5.

In Python, if you want to start counting from the end of the string (instead of the beginning) you can use negative indexes. For example, an index of –1 refers to the right-most character.

In the text «Hello World», the position (using negative indexes) of each character is shown here.

An example is shown here.

a = "Hello World"

print(a[-1]) #it displays the last letter
print(a[-3]) #it displays the letter r

Another way of extracting single characters from strings in Python is to unpack them into individual variables as shown here.

name = "Zeus"

a, b, c, d = name

print(a)     #It displays the letter Z
print(b)     #It displays the letter e
print(c)     #It displays the letter u
print(d)     #It displays the letter s

Notice: This last method requires you to know in advance how many characters are in the string. If the number of variables you supply does not match the number of characters in the string, Python displays an error.

In Python, if you wish to extract a portion of a string you can use the following formula:

subject[ [beginIndex] : [endIndex] [: step]]

This returns a portion of subject. Specifically, it returns the substring starting from position beginIndex and running up to, but not including, position endIndex. Both arguments beginIndex and endIndex are optional. If beginIndex is omitted, the substring starting from position 0 and running up to, but not including, position endIndex is returned. If endIndex is omitted, the substring starting from position beginIndex until the end of subject is returned.

Notice: “Slicing” in Python is a mechanism to select a range of elements (here characters) from a sequence (here a string).

The last argument step is optional as well. If omitted, its default value is 1. If supplied, it defines the number of characters you want to move forward after each character is retrieved from the original string.

An example is shown here.

a = "Hello World"

print(a[7:9])       #It displays the letters o, and r
print(a[4:10:2])    #Step is set to 2. It displays the letters o, W, and r
print(a[7:])        #It displays the letters o, r, l and d
print(a[:3])        #It displays the letters H, e, and l

If you want to start counting from the end of the string (instead of the beginning) use negative indexes as shown here.

a = "Hello World"

print(a[3:-2])      #It displays "lo Wor"
print(a[-4:-2])     #It displays the letters o, and r
print(a[-3:])       #It displays "rld"
print(a[:-3])       #It displays "Hello Wo"