3.2.6 String Handling Operations in a Programming Language
Table of Contents
- 1. String Handling
1 String Handling
Learn It: What does string handling mean?
Strings – A string is any sequence of characters from the keyboard, including digits, letters, punctuation and any other characters you can see when you look at your keyboard. It is a very important data structure in programming because it is used to achieve many things.
Casting – Converting a piece of data to a specific data type. For example, a user might enter a string and the program might convert it to an integer in order to perform certain calculations.
Typical operations on strings
- Because strings are so important, programmers need to know how to carry out a number of frequently used operations on them. These include:
- Finding the length of a string.
- Finding the position of a character.
- Getting a substring (also called a slice) from a string.
- Concatenation of strings.
- Converting a character into its ASCII character code.
- Converting an ASCII character code into a character.
- Converting digits between string digits and numerical digits.
- Strings are a useful way of storing any data that you
do not need
to perform calculations on. - However, there are times when you need to be able to select a part of a string,
manipulate
a string or convert it to another data type.
Learn It: Length
Length:
- Using the Python code, we can ask the user for a name and then using the
LEN function
find out the length of the name as follows:
- Entering the name “George” then the
LEN function
would return “6”.
Learn It: Position
Position
- Position will find out where in the
string a character appears
. Don’t forget it will startcounting from 0
. - In this example, it will find the letter “o” in the
name
and display thenumber
of theposition
.
- With the name “George” the find function will return “2”.
- In Python, if the letter cannot be found it will return the value of
-1
.
Learn It: Substring
Substring
- A substring is a group of two or more characters that appears
inside another string
. - For instance, “the best of” is a
substring of
“It was the best of times”. - The substring can be found as long as you know the
start position
and theend position
of the string you arelooking for
.
- Don’t forget Python needs an
extra position counted
at the end,otherwise it will stop
when it reaches the last letter and not show it. - This substring will return “ngo”.
- It is assumed that the
start position
is0
andnot 1
, somessage[ 0 ]
contains thefirst character
of the string “M”.
Try It: Substrings
- Write some pseudocode so that the user types in a message and then they are asked for a letter in that sentence.
- It should then display from that letter to the end of the message.
- Once you have written it in pseudocode create it in Python, save it with a sensible name and test it out.
Learn It: Concatenation
Concatenation
- Concatenation means
joining two strings together
and this is done using the+ symbol
in both pseudocode and Python.
- Please note, the “ “ has been
added
in themiddle
tojoin the names
with a space between them.
Badge It: Pseudocode Challenge
Silver - Data Type Conversions
- Write pseudocode to assign a 7-digit number to an integer variable.
- Convert this to a string, and output the middle three digits.
Upload to Fundamentals of 3.2 Programming - 3.2.6 String Handling Operations in a Programming Language: Silver on BourneToLearn
Learn It: ASCII Character codes
ASCII Character codes
- ASCII stands for “American Standard Code for Information Interchange”. Using this code, every character can be represented as a
7-bit binary pattern
, allowing128 different characters
to be represented. - The table below shows how each upper and lower-case letter is represented along with numbers and some symbols:
- If you want to convert a
character
into anASCII code
, you can convert that character to the corresponding ASCII code as shown below:
- If you want to convert an
ASCII code
into acharacter
, you can convert this as shown below:
Learn It: String Conversions
String Conversions
- Sometimes we need to
change
thedata type
of a variable from astring
to aninteger
or areal (float)
number or vice versa.
- Python is quite simple and does not really care about the
current data type
, it only needs to knowwhat
youare converting it to
(int, float or str). - Whereas the pseudocode is much
more specific
andneeds you to know
the current data type and the required data type you are converting it to.
Badge It: Concatenation Challenge
Gold - Concatenation
- Create a Python program which will ask the user to enter their date of birth in the format dd/mm/yyyy.
- It should also ask them to enter their first name and then enter their surname.
- The program should take the digits for the date of birth and add them together (for instance if the date was 13/02/2002 it should add together 1 + 3 + 0 + 2 + 2 + 0 + 0 + 2 = 10).
- It should join the first 3 digits of their surname to the last letter of their first name and join this to the number that was created. So, if their name is Jemima Puddle-Duck and their date of birth was the 13/02/2002 the program should display Puda10.
- Make sure you save the program with a sensible name and test it out.
Upload to Fundamentals of 3.2 Programming - 3.2.6 String Handling Operations in a Programming Language: Gold on BourneToLearn
Badge It: ASCII Challenge
Platinum - ASCII Challenge
- Write pseudocode statements to accept an uppercase character from the user, and output the next character in the alphabet.
- If the user inputs Z, the letter A should be outputted.
- Now create a Python program for this task.
Upload to Fundamentals of 3.2 Programming - 3.2.6 String Handling Operations in a Programming Language: Platinum on BourneToLearn