String Handling
Course Content
Understand the concept that strings are immutable
Create and use substrings
A string is simply an array of characters. And in Python can be show in various forms:
They can be indexed like an array starting from [0]:
So for example:
Gives us the following output:
e
Substrings
A substring is just a small piece of a larger string.
To access this we use a startindex and an endindex so using our previous example:
Gives us the following output:
The
You will notice that Python stops BEFORE it reaches the end index
Some further examples
If we use the string above:
Gives us the following output as it starts from character 2 and stops before it gets to character 4:
am
The example below starts from position 7 and carries on until the end of the string, which we have found using the len function.
Gives us the following output:
string
Although print(original[7:]) would provide the same output. As we have not specified the end position it will continue until the end of the string.
If we wanted to create a substring from the start of the string we could use the following code:
Gives us the following output:
I am
Although print(original[:4]) would provide the same output. As we have not specified the start position before the : then the substring will be from the start of the string.
Extracting from the right of a string
Some languages have a right(string,startposition, endposition) function that allow you to extract a substring from the end of a string.
Such as:
mystring = "This string is my original"
SEND right(mystring,8)
Gives us the following output:
original
>>>
Python doesn't have a right function but can use a negative indexing system to extract from the end of a string.
So for example:
Gives us the following output:
r
>>>
If we want more than one string we need to specify start and end positions, as in earlier examples the start and end position can be left blank if is the start of the string or the end of the string respectively.
Gives us the following output:
string
Similar to loops you can also use a step argument:
Gives us the following output - as Python will print the characters from position 0 to before position 3 but increasing the position by 2 each time. So will print out the characters at position 0 and 2.
Ti
Reversing a String
If you want to reverse a string there is a Python specific method to do this:
Gives the following output
lanigiro ym si gnirts sihT
However the generic way would be to use a loop to inde each character in the string, starting from the end. One possible method is shown below:
You will notice that we start at the right most position (calculated using the len function - 1). The reason for the -1 from the length is that the length of a 3 character string is 3 but the highest index would be 2.
We then stop before we hit -1 ( which would be position 0) and we step through the loop in steps of -1.
Strings are immutable
They are immutable…This means they cannot be changed (they can be assigned a new value though).
So for example the ‘d’ in the string below should be a ‘s’
mystring = "I am a dtring"
So surely we can assign character at position 7 a new value of ‘s’
mystring[7] = "s"
TypeError: 'str' object does not support item assignment
>>>
Concatenation is a work around...
We could create a new variable to construct
Gives the following output:
I am a new string
So is building a new string at a time, this is the approach we used earlier when reversing the string.
Converting between Characters and ASCII
When manipulating strings it may be required to convert to an ASCII character or to a character from an ASCII value. Python has two predefined functions to achieve this.
For example
Will produce:
97
For example
Will produce:
A
You can use this to generate random characters using ASCII codes. A sample ASCII table is linked here
So we can use this to generate a random value between 65 and 90 and then grab the corresponding ASCII value to generate a random uppercase character such as below. This could be a useful technique for generating passwords etc.
Searching for characters
We have already covered linear searches, but this could be used to find when a particular character occurs in a string and then perhaps split it on that character - for example where a space is.
So for example:
Would generate the following output:
Forename = Mr
Surname = Hay
Tutorial Video - String Handling
![](https://www.google.com/images/icons/product/drive-32.png)