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

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

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++

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#

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

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

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.

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

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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This article is taken from a book of mine. If you enjoyed it, please visit my author's page inand check out all of my books!