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. We are learning ...
So that we can ...
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 ...
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.
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) 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' Can you explain what escaping means? Try and write another example if you can. 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 ... 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 ...
You should make notes on this. 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.
Make sure you have screenshot evidence of the string slice you used to get each of the fragments. 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.
Open up the Python programming environment, IDLE.
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' 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)) Manually count the characters in the string - do you get the same answer? 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 ...
Remember to evidence your work. Write about what you have found out. 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 Try to find some other substrings in message . Remember to evidence your work.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) Write about a situation where this might be useful. 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.
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 work. Remember to evidence your work. Consider using a table to present your results. 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.
In the Python programming environment, IDLE, type the following instruction and press the Enter key. >>> message = 'we can change strings using COOL string functions' 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()) 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! 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.
Open up the Python programming environment. >>> print(chr(70)) >>> print(ord('F')) Write about what happened and try to explain what the chr() and ord() commands do.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.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! 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.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) Can you see how the operation of + and , is different? Write your explanation in your notebook.How about this? Type each instruction followed by the Enter key. >>> age = 12 >>> print('My age is',age) 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!
Click to load key word list to help you make your own flash cards
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.
|