Part 6: String Manipulation

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 58

Introduction to Python

Programming

Part 6: String
Manipulation
How to use this resource
Skill Explanations: Pink Slides
• Within each unit, you will learn how to develop
and apply a range of Python programming skills.
Skill explanations are in pink.
Tasks: Rookie Pro Beast
• After reading the explanation of a skill, apply it
within the given tasks. The tasks are categorised
into Rookie (Easy), Pro (Medium) or Beast (Hard).
Challenges: Rookie Pro Beast
• Once you have learned how to apply your new
skills, demonstrate your ability by completing the
given challenges. Don’t forget the debugging task!
• Once complete, review your performance!
Learning Objectives
Rookie
• To be able to use a variety of string manipulation techniques with assistance.
• To be able to be able to state what changes have happened to the string.
Pro
• To be able to use a variety of string manipulation techniques with some assistance.
• To use manipulation techniques to reduce programming logic errors.
Beast
• To be confident in applying a variety of techniques with little assistance to make changes to
stored string statements.
• To use manipulation techniques to improve the robustness of programming.
Skill Contents
Once you complete a skill, colour code
Contents Confidence
the box to show your level of Level
confidence. You can always revisit the Concatenation
skill later on to boost your confidence. Changing the casing (upper & lower)
Find the length of stored string
Key Colour
Code Title text
Count specific elements of a string
I am very good at this skill
and could show a friend Using the index
how to do it. Replacing an element of a string
I am confident I can Check string and create a Boolean
perform this skill on my response
own.

I need to improve my
understanding of this skill.
What does string manipulation mean?
• String manipulation simply means altering, adjusting or measuring
existing string statements.
• In Python, string stored within variables can be altered and changed
in a variety of ways.
• This guide will show you some of the most popular ways to
manipulate stored string statements.
Skill 1: Concatenation
• Concatenation means to link two or more things together.
• With regards to Python, it means to join two or more string variables together.
• For example;

Variables a, b, c and In this code,


d all contain string variable e includes
which is space characters
concatenated between each
within variable e. variable which
The output is means the output is
shown below; much clearer.
Task 1: Rookie
1. Create two variables to store the words Hello and World in.
2. Concatenate the words suitably within a third variable and output it.

Pseudo code Python

START
var1 = Hello
var2 = World
var3 = var1 + var2
OUTPUT: var3
END
Skill Task 1: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 2: Pro
1. Create two variables called first and last.
2. Ask for the users first name and last name and store the user input within the appropriate variable.
3. Concatenate the two variables within a third variable suitably and output the third variable.
Flowchart Pseudo code

START
OUTPUT: What is your first name?
first = user INPUT( )
OUTPUT: What is your last name?
last = user INPUT( )
full = first + last
OUTPUT: Hello + full
END
Skill Task 2: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Skill 2: Change text to upper or lower case
• If you were checking if the user input “hello world” was equal to the string
statement “Hello World” it would return untrue.
• This is because string comparisons are case sensitive.
• By manipulating string using the .upper and .lower python functions
Variable a contains Variable b contains
the lowercase string the uppercase
“hello world”. string “HELLO
The .upper( ) WORLD”.
function converts all The .lower( )
lowercase string function converts all
into uppercase uppercase string
string as shown in into lowercase
the output: string as shown in
the output:
Task 3: Rookie
1. Create a variable and add your name as string.
2. Create another variable and change the string to uppercase.
3. Print the second variable.
Pseudo code

START
var1 = Bob
var2 = change var1 to UPPERCASE
OUTPUT: var2
END
Skill Task 3: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 4: Pro
1. Ask the user what the capitol of England is. Store the response in a variable.
2. Change the response so that it is all in lowercase.
3. Using an IF statement, if the user’s answer is equal to london, then let them know they are correct, else let them know they are
wrong.

Flowchart Pseudo code

START
OUTPUT: What is the capital of England?
answer = user INPUT( )
answer2 = answer in lowercase.
IF answer = london:
OUTPUT: Correct!
ELSE:
OUTPUT: Wrong!
END
Skill Task 4: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Skill 3: Find the length of a string
• It is possible to count how many characters are included within a string
statement.

Variable a contains a string statement.


Variable b checks the length of
variable a and stores the result as in
integer.

As you can see, when variable b is


printed, the integer length of a is
outputted.
Task 5: Rookie
1. Create a variable and add your name as string.
2. Create another variable and store the length of the string in the first variable.
3. Print the second variable.

Pseudo code Python

START
var1 = Bob
var2 = length of Bob
OUTPUT: var2
END
Skill Task 5: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 6: Pro
1. Ask a user to input their first name and last name. Store them in separate variables.
2. Concatenate the variables within a third variable.
3. Measure the length of the user’s first and last names and output both the users name and length within a sentence.

Flowchart Pseudo code

START
OUTPUT: What is your first name?
first = user INPUT( )
OUTPUT: What is your last name?
last = user INPUT( )
full = first + last
len_full = length of first + length of last
OUTPUT: Hello + full + your name is + len_full + characters long!
END
Skill Task 6: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 7: Beast
1. Ask the user to input a password between 6 – 12 characters in length. Store the password in a variable.
2. Store the length of the password in another variable.
3. If the password is too short or too long, output a suitable message.
4. Otherwise, let the user know their password has been accepted.

Flowchart Pseudo code

START
OUTPUT: Please enter a password between 6-12 characters long.
pw = user INPUT( )
lenPW = length of pw
IF lenPW <6:
OUTPUT: Your password is too short.
ELSE IF lenPW >12:
OUTPUT: Your password is too long.
ELSE:
OUTPUT: Password accepted.
END IF
END
Skill Task 7: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Skill 4: Title text
• The function .title( ) can be used to convert each word of a string statement so
that the first letter is placed in uppercase and the rest of the string into
lowercase.
• See the examples below:

Variable a contains the In contrast, variable b


lowercase string “hello contains the uppercase
world”. string “HELLO WORLD”.
The .title( ) function will The .title( ) function will
convert the first letter again convert the string
of each word to as it did to variable a;
uppercase and the rest
to lowercase as shown
in the output:
Task 8: Rookie
1. Create a variable and add your name as string.
2. Create a second variable, use the title function and store the string in the first variable in it.
3. Print the second variable.

Pseudo code Python

START
var1 = bob
var2 = title string in var1
OUTPUT: var2
END
Skill Task 8: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 9: Rookie
1. Create a variable and add your full name as string.
2. Create a second variable, use the title function and store the string in the first variable in it.
3. Print the second variable.

Pseudo code Python

START
var1 = david beckham
var2 = title string in var1
OUTPUT: var2
END
Skill Task 9: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 10: Pro
1. Create two variables and store a user’s first and last names within them.
2. Create a third variable, use the title function and concatenate the previous two variable in it.
3. Print the second variable within a suitable string output.

Flowchart Pseudo code

START
OUTPUT: What is your first name?
var1 = user INPUT( )
OUTPUT: What is your last name?
var2 = user INPUT( )
var3 = title (var1 + var2)
OUTPUT: Hello + var3 +, pleased to meet you!
END
Skill Task 10: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Skill 5: Count elements of a string
• The .count( ) function can be used to find a specific string element, or
elements, within a variable.
• The examples below show how it can be used;

Variables a, contains The .count( )


the string statement function can also be
“hello world”. Variable used to find
b contains the count of sequences of string,
the letter “l”. As you as shown in the
can see, when variable following example;
b is printed, the
number 3 is outputted.
Task 11: Rookie
1. Create a variable and add your name as string.
2. Create another variable and add a count of a specific letter, for example ‘a’.
3. Print the second variable.

Pseudo code

START
var1 = kelly holmes
var2 = count of the letter “m”
OUTPUT: var2
END
Skill Task 11: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 12: Pro
1. Ask the user to input their name. Store this in a variable.
2. Ask them which letter they want to count. Store this in a variable.
3. Conduct the count using the user variable and output the answer in a sting sentence.

Flowchart Pseudo code

START
OUTPUT: What is your first name?
name = user INPUT( )
OUTPUT: Which letter do you want to check?
letter = user INPUT( )
count = count of number of times letter appears in name
OUTPUT: The number of times + letter + appears in + name + is + count
END
Skill Task 12: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 13: Beast
1. Ask a user to input their first name. Store it in a variable. Convert the name to lower case in another variable.
2. Take a count of each vowel (a, e, i, o and u) and store each count in a separate variable. Also take a count of the total vowels.
3. Output the information in suitable string statements, showing the user how many individual and total vowels their name contains.

Pseudo code

SSTART
OUTPUT: What is your first name?
name = user INPUT( )
lower = convert name into lower case
count1 = count of the letter a in lower
count2 = count of the letter e in lower
count3 = count of the letter i in lower
count4 = count of the letter o in lower
count5 = count of the letter u in lower
total = total of Count1 to Count5
OUTPUT: Your name contains the following vowels:
OUTPUT: a: + Count1 (repeat for Count2 to Count5).
OUTPUT: In total your name contains + total + vowels.
END
Skill Task 13: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Skill 6: Using the Index
• When data is allocated to a variable, each piece of data is given a numbered
index.
• For example, if the text “Hello World” was assigned to a variable, each string
would be allocated the following index locations:

Index 0 1 2 3 4 5 6 7 8 9 10
Variable a H e l l o W o r l d

In this code, variable a is Note from the examples


allocated the string “Hello how indexing begins at 0.
World”. To select an item Also not that space
within a specific indexed characters are also
location, the square brackets allocated an index place.
[ ] are used as shown within
the example;
Task 14: Rookie
1. Create a variable and add your name as string.
2. Create another variable and store the first index value of the first variable.
3. Output the second variable.

Pseudo code Python

START
var1 = Sharon
var2 = var1 index[0]
OUTPUT var2
END
Skill Task 14: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 15: Pro
1. Ask the user to input their first name and last name. Store these in separate variables, converting both to titled string.
2. Use indexing to identify the first letter of both the user’s first name and last name. This will identify their initials.
3. Output the user’s initials in a suitable sting sentence.

Flowchart Pseudo code

START
OUTPUT: What is your first name?
first = user INPUT( )
OUTPUT: What is your last name?
last = user INPUT( )
indexFirst = first index[0]
indexLast = last index[0]
initials = indexFirst + indexLast
OUTPUT: Your initials are + initials
END
Skill Task 15: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Skill 7: Replacing string
• The .replace( , ) function allows you to replace one string character
with another. It works as follows;

variable.replace(old string , new string)

In the first example, The second


you can see how example shows how
the first letter data within a
within the replace variable can be
function is replaced used to replace a
with the second string character;
letter;
Task 16: Rookie
1. Create a variable and add your name as string.
2. Replace a letter of your choice with another of your choice. Store this in a new variable.
3. Output the result.
Pseudo code Python

START
var1 = Bob
var2 = within var1 replace “b” with “p”
OUTPUT: var2
END
Skill Task 16: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 17: Pro
1. Ask the user to input their name. Store this in a variable.
2. Ask them which letter they want to replace. Store this in a variable.
3. Ask them which letter they want to replace it with. Store this in a variable.
4. Replace the letters and store the resultant name in a separate variable. Output the new name within a sentence.

Flowchart Pseudo code

START
OUTPUT: What is your name?
name = user INPUT( )
OUTPUT: Which letter do you want to replace?
letter = user INPUT( )
OUTPUT: Which letter do you want to replace it with?
new = user INPUT( )
newName = within name replace letter with new
OUTPUT: Your new name is + newName
END
Skill Task 17: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Skill 8: Create a Boolean response if a string character is
present
• Assume you had to create a password which had to contain a specific
character, this check could do just that.

In this example, The second


variable b stores example shows how
the response to the data within a
check for the letter variable can also be
“l” within the checked against the
variable a. As there string;
are three l’s, the
result of the check
is true.
Task 18: Pro
1. Store a random word in a variable.
2. Ask the user to guess a letter in the mystery word.
3. Check if the user input is in the mystery word. Using an if statement, output a suitable response depending on if the
letter is present or not.

Flowchart Pseudo code

START
word = python
OUTPUT: Guess a letter in the mystery word!
guess = user INPUT( )
IF guessed letter is in word:
OUTPUT: Good guess!
ELSE:
OUTPUT: Bad guess!
END IF
END
Skill Task 18: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Programming Challenges

• Now you have learned some programming skills, it’s time to put them
into practice!
• Complete the following programming challenges. Start with the
Rookie challenge and see how far you can push yourself.
• Finally, complete the debugging challenge.
• Once you have finished your challenges, complete the review. Look
back at the skills you have learned so far to help you solve the
challenge.
Add a print screen of
your solutions to the
Programming Challenge: Rookie next slide.

• Write a piece of code that asks a user for their first name and
last name then titles and concatenates it within another
variable.
• Count the number of letters in the users name and store it in
another variable.
• Once done, output the gathered information in a sentence
like this; 1 Mark
Bonus mark
for adding
comments!
Programming Challenge: Rookie - Answer
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Add a print screen of
your solutions to the
Programming Challenge: Pro next slide.

• Write a piece of code that asks for the user’s first


and last names. Output the following information
within a suitable format;
• Full name titled
• Number of letters within their name (excluding
1 Mark
spaces)
• Their initials Bonus mark
for adding
comments!
Programming Challenge: Pro - Answer
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Add a print screen of
your solutions to the
Programming Challenge: Beast next slide.

• Write a piece of code that asks for the user’s first and last
names. Output the following information within a suitable
format;
• Full name titled
• Number of letters within their name (excluding spaces)
• Their initials 1 Mark
• Number of individual and total vowels within their Bonus mark
name. for adding
comments!
Programming Challenge: Beast - Answer
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Debugging Question

Which of the following lines of code below would output the letter B :

Highlight the correct cell in green:

1 Mark
Complete the table below to help you identify what
Challenges Review areas you are confident at and which areas you need
to improve further:
Challenge Your Score (Max 1 mark each)
Rookie Challenge
Pro Challenge
Beast Challenge
Debugging Challenge

Unit Review
What Went Well:

Even Better If:

You might also like