### CS06 : String Manipulation

In this topic, you will learn about one of the most fundamental datatypes, strings. Apart from learning what strings are, you will also learn how to find out about them and manipulate them.

 Learning OutcomesString Manipulation

We are learning ...
• About strings
So that we can ...
• Describe what strings are
• Have practical experience determining the characteristics of strings
• Have practical experience of manipulating strings
- Position
- Substrings
- Concatenation
• Perform character and string conversions
- Character ⇔ Code
- String ⇔ Integer
- String ⇔ Float

 Activity 1What are strings

In programming, strings are lists of characters.

Strings are always given in quotation marks - either single or double will do as long as you are consistent.

`"This is a string surrounded by double quotes"`
`'This is a string surrounded by single quotes'`
`'This string will confuse the computer"`
`"and so will this one!'e`

Knowing how to handle strings is really important because, generally, all data which a computer is given is in the string form, especially if it's typed at the keyboard. There are two basic skill areas ...
• Handling native strings, finding out about them, processing them
• Converting strings into other data types to enable processing

Task 1.1 Creating and investigating the structure of strings
Where we learn how to use Python to create strings and manipulate them

Use the following exercises to learn about how Python handles strings. You should evidence the work you have done in a word processed document with a combination of screenshots and written explanation.

Type code in, do not copy and paste.

Get ready to code

Open up the Python programming environment, IDLE.

At the prompt

Type the following, pressing  Enter  after each line and twice at the end. Note : The first command does do something - it saves the string in the computers memory and assigns a variable name to it. Remember to evidence your work.

`>>> message = 'Hello, World!'`
` >>> for character in message:`` print(character)`

In your notebook / on paper

Can you explain what happens in your own words?
Rubber duck this simple script with your shoulder partner to help you to write your explanation. Quack, quack!

At the prompt

If we wish to use single (or double) quotes inside a string, we often have to escape them using the backslash character. Type the following assignment statements, pressing the  Enter  key after each one, noting what happens in each case. Remember to evidence your work.

`>>> saying = 'I didn't do it, honest I didn't'`
` >>> saying = 'I didn\'t do it, honest I didn\'t'`

In your notebook / on paper

Can you explain what escaping means? Try and write another example if you can.

At the prompt

We can access individual characters in the string like we would access items from a list. Try the following example at the prompt (make sure you still have `message` defined from the statement above). Remember to evidence your work.

`>>> print(message[0])  # Gives the letter 'H'`
`>>> print(message[4])  # Gives ...`
`>>> print(message[10]) # Gives ...`
`>>> print(message[13]) # Gives ...`

In your notebook / on paper

Can you explain the result of the last instruction?

We can also use list slicing to return parts of the string. We use colons (:) to separate the starting and ending indices from the string. For example ...

`string[start:end+1] ` returns a substring starting at start and ending at the end
`string[start:]      ` returns a substring starting at start to the end of the string
`string[:end+1]      ` returns a substring from the start of the string, ending at end
`string[:]           ` returns the whole string (what's the point of this?)

You should make notes on this.

At the prompt

Be careful - you must always add one to the end character index or you will miss off the last character. Enter the following line and press the  Enter  key. Remember to evidence your work.

`>>> message = 'This is an example of string slicing.'`

For the following exercises, you must derive (come up with) a slicing operation to produce the stated substring. The first one has been done for you (to help). Remember to evidence your work.

`'example'         ` is returned from `print(message[11:18])`
`'string'          ` is returned from ...
`' of '            ` is returned from ...
`'string slicing.' ` is returned from ...
`'This'            ` is returned from ...

In your notebook / on paper

Make sure you have screenshot evidence of the string slice you used to get each of the fragments.

 Activity 2Finding out things about strings

Strings are 'objects' in any programming language and, as such, have properties such as length.

Task 2.1 Finding out about strings
Where we learn how to use Python to find out about the characteristics of strings

We can use tools in Python to find out about our string. For the following exercises, carry out the suggested tasks to learn what Python knows about strings. Evidence your work in a suitable word processed document using a combination of screenshots and written explanation. Type code in, do not copy and paste.

Get ready to code!

Open up the Python programming environment, IDLE.

At the prompt

In the Python programming environment, IDLE, type the following instruction and press the  Enter  key. Remember to evidence your work.

`>>> message = 'In this section, we will find out about strings'`

At the prompt

One of the simplest things we can do with a string is to find it's length. Try this (and remember to evidence your work)

`>>> print(len(message))`

In your notebook / on paper

Manually count the characters in the string - do you get the same answer?

At the prompt

We can use `string.count(substring)` to count how many times a certain character or substring appears in our original string. Try the following exercises in the REPL. The first one has been done for you ...

The number of times '`i`' appears in message is found using `message.count('i')` which is `5`
The number of times '`s`' appears in message is found using ...
The number of times '`we`' appears in message is found using ...
The number of times '`string`' appears in message is found using ...

Remember to evidence your work.

In your notebook / on paper

Write about what you have found out.

At the prompt

If we just want to know whether or not a certain substring (word) is in a message, we can use the `in` operator. The `in` operator will return `True` if the substring is present and `False` if it isn't. Try typing the following statements, pressing the  Enter  key after each one. Remember to evidence your work.

`>>> 'will' in message`
` >>> 'in' in message`
` >>> ' ' in message`
` >>> 'cheese' in message`

In your notebook / on paper

Try to find some other substrings in `message`. Remember to evidence your work.

At the prompt

We can convert a sentence into a list of words using the `string.split()` function This might be useful if we want to perform some operation with the individual words in a sentence. Try typing the following, pressing the  Enter  key after each line. Remember to evidence your work.

`>>> wordList = message.split()`
` >>> print(wordList)`
` >>> wordList.sort()`
` >>> print(wordList)`

In your notebook / on paper

Write about a situation where this might be useful.

At the prompt

This might seem a little strange but we can also check what type a string is. A string is a string, obviously, but the characters in the string could actually be some other type. This is important when we are asking the user for information at the prompt because that is always supplied as a string. Type the following commands, pressing the  Enter  key after each line. Remember to evidence your work.

`>>> word = 'Thrutch'`
`>>> name = 'John Smith'`
`>>> password = 'password123'`
`>>> gross = '144'`
`>>> price = '10.99'`
`>>> emoji = '^o.0^'`

For each of these strings, carry out the following checks (you have to do 18 checks altogether). Each one should produce `True` or `False` as a response.

`string.isalpha()` will return `True` if the `string` only contains letters
`string.isalnum()` will return `True` if the `string` contains only letters and / or numbers
`string.isdigit()` will return `True` if the `string` contains only numbers

Replace the italisised word string with the actual variable name of the string. You need to leave the brackets in at the end or else the method will not workRemember to evidence your work. Consider using a table to present your results.

 Activity 3Altering strings

I can't change the individual characters in a string. We say that strings are immutable objects. I can

Task 3.1 Altering strings
Where we learn how to use Python to change strings

Use the following tasks to help you learn how Python can be used to alter strings. Evidence your work in a suitable word processed document using a combination of screenshots and written explanation. Type code in, do not copy and paste.

Get ready to code

In the Python programming environment, IDLE, type the following instruction and press the  Enter  key.

`>>> message = 'we can change strings using COOL string functions'`

At the prompt

We can change the case of strings. Type each command followed by the  Enter  key.

`>>> print(message.lower())`
` >>> print(message.upper())`
` >>> print(message.capitalize())`
` >>> print(message.title())`

Write about a situation where this may be useful.

At the prompt

If we are happy with the type of our strings, we can change them into other variables so we can do useful work. This is known as type conversion and is used a lot in programming.

Try typing the following sequence of instructions following each one with the  Enter  key. You should get red error messages when you try to execute the second line in each code block. Can you explain why this is and how the error was fixed?

`>>> gross = '144'`
` >>> print(gross+12)`
` >>> gross = int(gross)`
` >>> print(gross+12)`

`>>> price = '10.99'`
` >>> quantity = 7`
` >>> print(price*quantity)`
` >>> price = float(price)`
` >>> print(price*quantity)`

`>>> age = 12`
` >>> print('My age is '+age)`
` >>> age = str(age)`
` >>> print('My age is '+age)`

Make sure you try to explain what it happening in each coding example!

 Activity 4Character to code conversions

Task 4.1 Stringy ASCII things
Where we learn how to use Python to convert characters into ASCII and vice versa

We've met this already in the section on character encoding but we'll revisit it here - it does not harm. Use the following tasks to help you learn how Python can be used to convert characters / strings to ASCII and vice versa. Evidence your work in a suitable word processed document using a combination of screenshots and written explanation. Type code in, do not copy and paste.

Get ready to code

Open up the Python programming environment.

At the prompt
: Type the following instructions at the console, pressing  Enter  after each one ...

`>>> print(chr(70))`
` >>> print(ord('F'))`

In your notebook

Write about what happened and try to explain what the `chr()` and `ord()` commands do.

ASCII Converter

I have written a Python program called 'ASCIIConverter.py'. Download it, save it to a suitable place in your documents, right click on the file and 'edit with IDLE'. Run it in your Python programming environment (Run > Run Module or press F5). It will appear as though nothing has happened. Now type the following commands exactly in the console, pressing  Enter  after each one.

`>>> ASCIIToString([72,101,108,108,111,33])`
` >>> StringToASCII('Goodbye!')`

`ASCIIToString()` is a user defined function. We'll learn more about functions later in the course.

In your notebook

Inspect the code in the script and try to explain out what it does (this is called Rubber Ducking) and write down your ideas in your notebook. You may wish to use screenshots to help your explanation. If you are struggling to do this on your own, work in a pair and explain the script to each other a line at a time! Honestly, it helps!

One situation where `chr()` and `ord()` might be useful would be calculating a hash value for a string. A hash value can be used to locate a string in a list or verify it's integrity.

Try running the Python code in the Trinket. It runs in the browser so you don't need IDLE installed.

The value that the code gives could be used to store your message in a list - the length of the list is given by the number 100 on line 7. If you wanted to find the message again, you can rehash it and look in the list using the hash value it gives - cool!

 Activity 5Joining strings

Task 5.1 Joining strings
Where we learn how to use Python to join strings together

In the last exercise, we joined two strings together using a plus sign (`+`). This is called contatenation and is a very common thing to do during programming. You can also use the comma (`,`) to join strings together in Python but they behave slightly differently. Use the following tasks to help you learn how Python can be used to join strings. Evidence your work in a suitable word processed document using a combination of screenshots and written explanation. Type code in, do not copy and paste.

At the prompt

In the Python programming environment, IDLE, type the following instruction and press the  Enter  key.

`>>> forename = 'John'`
` >>> surname = 'Smith'`
` >>> print(forename+surname)`
` >>> print(forename,surname)`

In your notebook

Can you see how the operation of `+` and `,` is different? Write your explanation in your notebook.

At the prompt

How about this? Type each instruction followed by the  Enter  key.

`>>> age = 12`
` >>> print('My age is',age)`

In your notebook

Compare this to the last time you tried to join an integer with a string in the last activity. Describe the advantage of using a comma to join strings together.

That's all for now - we'll look at formatting output in a later section. Phew!

 Assessment Task (Homework) You have seen lots of examples of the ways in which we can examine strings and manipulate them. I would like you to come up with a series of 5 your own examples to illustrate some of the things that you have learnt. Write them in your notebooks together with a simple explanation of what each one shows. Grading rubric MASTER :  You have come up with 5 suitable examples which demonstrate some of the things you have learnt. Your explanations are clear and concise. APPRENTICE : You may have used similar examples to mine but only from the first activity. You have written about them. NOVICE : You have copied some of my examples but have written about them.

Click to load key word list to help you make your own flash cards

 Hungry for more?

What's going on here?

Copy and paste the following string and use the Python `len()` function to work out how many characters long it is ...

ZZ​​​​​ ZZ

That's strange ... can you find out what's going on? You might want to use this website to help you :)

Keep on finding out about strings

We've only really scratched the surface with strings. Find out more about string operations at Tutorials Point Strings section or by searching the web for python strings.