String manipulation
Strings are a collection of charactersData type that holds a single character. stored under one identifier. They can be thought of as an array of characters. There are a variety of different ways we can manipulate strings.
As a string behaves like an array of characters, we can access individual characters in a string. Strings are referenced by indexA data structure in a database that groups a set of records associated with a keyword., starting at 0.
name = 'walter'
name[0] = w
name[1] = a
name[2] = l
name[3] = t
name[4] = e
name[5] = rConcatenation
Strings can be joined together to form a new string of data. This is called concatenation. We use the + symbol to concatenate strings. For example:
fname = 'walter'
sname = 'white'
name = fname + ' ' + snameThis will produce the output: walter white. Note the space (' ').
Character searching
We can determine if an individual character is found in a string using the 'in' keyword. This will return True/False:
'f' in sname = False
'e' in fname = TrueThis also works with substrings (subset of characters):
'alt' in fname = TrueString splitting
It is possible to find part of a string by using a subString (a bit, or part, of a string) method. Remember that the first character in the string starts at reference 0.
For example, select a substring from character 2 to character 4 (remember, the slice will stop before element 4):
fname.subString(2,4) = ltIn Python, this can be implemented as:
fname[2:4] = ltPython provides the ability to slice strings. Slicing characters from a string allows programmers to select any characters that they want from a string.
You can set the element you want to start the slice at, which element you want to finish at, and the increments or decrements you want. For example:
fname[0:5:2] = wleThis returns a character, then moves forward 2 positions and returns the next character until character 5.
Common string manipulation techniques
| Command | Description | Example | Output |
| .length | This returns the number of characters in the string. This will include spaces | fname.length | 6 |
| .upper | This changes all the characters in the string to upper case | name.upper | WALTER |
| .lower | This changes all the characters in the string to lower case | name.lower | walter |
| [n] | This selects a particular character in the string | fname[2] | l |
| .subString(x,y) | This selects a particular part of a string. It starts at the place in the string set by x and selects the next character, set by y | fname.subString(1,2) | al |
| str() | This converts any data into the string data type | str(fname) | - |
| ASC | This will return the ascii number of the character | ASC(fname[0]) | A - 119 |
| Chr | This will return the ascii character of a number | CHR(65) | A |
| Command | .length |
|---|---|
| Description | This returns the number of characters in the string. This will include spaces |
| Example | fname.length |
| Output | 6 |
| Command | .upper |
|---|---|
| Description | This changes all the characters in the string to upper case |
| Example | name.upper |
| Output | WALTER |
| Command | .lower |
|---|---|
| Description | This changes all the characters in the string to lower case |
| Example | name.lower |
| Output | walter |
| Command | [n] |
|---|---|
| Description | This selects a particular character in the string |
| Example | fname[2] |
| Output | l |
| Command | .subString(x,y) |
|---|---|
| Description | This selects a particular part of a string. It starts at the place in the string set by x and selects the next character, set by y |
| Example | fname.subString(1,2) |
| Output | al |
| Command | str() |
|---|---|
| Description | This converts any data into the string data type |
| Example | str(fname) |
| Output | - |
| Command | ASC |
|---|---|
| Description | This will return the ascii number of the character |
| Example | ASC(fname[0]) |
| Output | A - 119 |
| Command | Chr |
|---|---|
| Description | This will return the ascii character of a number |
| Example | CHR(65) |
| Output | A |