0% found this document useful (0 votes)
42 views

Lesson 1 - String Manipulation - Hard

The document provides information about string manipulation in Python. It discusses concatenation using + to join strings together with or without spaces. It describes using len() to find the length of a string. It also covers substrings and indexing strings to extract parts, with examples of using positive and negative indexes. Tasks at the end involve string manipulation concepts like concatenation, length checks, uppercase/lowercase conversion, and substrings.

Uploaded by

close6707
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Lesson 1 - String Manipulation - Hard

The document provides information about string manipulation in Python. It discusses concatenation using + to join strings together with or without spaces. It describes using len() to find the length of a string. It also covers substrings and indexing strings to extract parts, with examples of using positive and negative indexes. Tasks at the end involve string manipulation concepts like concatenation, length checks, uppercase/lowercase conversion, and substrings.

Uploaded by

close6707
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Revisit phase - Starter

Boolean expression True/False


4 > 5 AND 7 == 7 False
NOT(2 == 2) False
11 <= 2 OR 4 != 1 True
NOT(12 > 2 OR 5 < 1) False
5 != 5 AND 7 > 2 False
10 >= 10 and 5<=6 True
Lesson 6 Sunday, January 28, 2024

String Manipulation
Lesson objectives…
❑ Be able to manipulate strings.
❑ Apply string manipulations to
different python tasks.
❑Understand the purpose of substrings
and length checks.
Video
Lesson theory video: https://youtu.be/gEJMBkArksg
String manipulation - Concatenation
Concatenation means ‘joining’.

The ‘+’ sign joins together two or more string (without a


space).

firstname = input("Enter your firstname")


surname = input("Enter your surname")
print(firstname+surname)
String manipulation - Concatenation
As you saw on the previous slide the “+” joined two strings
together, however it did not add any space to it. If we wanted
to join things together with a space, we use comma “,”

firstname = input("Enter your firstname")


surname = input("Enter your surname")
print(firstname, surname)
Why not just use a comma all the time?

firstname = input("Enter your firstname")


surname = input(firstname, " Enter your surname")

The above code will give you an error, because you can’t add a
comma when storing a value in a variable because it will
count it as an argument. However you can concatenate using
“+” sign.
Correct way:

firstname = input("Enter your firstname")


surname = input("hey " +firstname+" Enter your surname")
String manipulation - Length

This example program asks the user to input a


name. It then finds the length of the string and
stores it in the nameLength variable. The length of
the string is displayed.
String manipulation - Length
len() is a python function that finds the length
of a string.
Example 1:
name = “thomas"
print( len (name) )
The above program will display the number 6. Because
that’s how many characters is in the string " thomas "
String manipulation - Length
len() is a python function that finds the length
of a string.
Example 2:
name = input("Enter a name")
print( len (name) )
The above program will diplay the number 4 if the user
types Jeff as the name.
Tasks
Complete task 58 – 61
Then complete any unfinished tasks from previous
lessons.
58. Create a program that:
• Asks the user for firstname.
• Asks the user for surname (use the firstname inside the question – example below)
• Display the firstname and surname in a full sentence.
• Use + sign to concatenate.
Paste your code below:
59. Create a program that:
• Asks the user for their favourite drink.
• Store the length of the string in a variable called “drinkLength”
• Display the length of the drink on the screen in a full sentence.
Paste your code below:
60. Create a program that:
• Asks the user for their firstname.
• Ask the user for their surname
• Concatenate the two strings(variables) together without space and store it in a variable
called “total”.
• Display the total length of firstname+surname.
Paste your code below:
61. Create a program that:
• Ask the user to enter a colour that contains 6 letters.
• If the user enters the correct answer. Display “well done”
• Otherwise display “incorrect”.
Hint: len(colour)
Paste your code below:
String manipulation – Case conversion

You can turn your string to either


uppercase using .upper() or lowercase
using .lower()
name = input("Enter your name")
print ( name.upper() )
print ( name.lower() )
String manipulation – Substring
Substrings are used to extract part of a string.
Index numbers starts from 0.
What do you think the following code will display?
country = "ENGLAND"
print(country[0])

It will display the letter “E”

Index 0 1 2 3 4 5 6
number
Characters E N G L A N D
String manipulation – Substring
-1 is used to select the last character of a
string.
What do you think the following code will display?
country = "ENGLAND"
print(country[-1])

It will display the letter “D”

Index 0 1 2 3 4 5 6
number
Characters E N G L A N D
String manipulation – Substring – PYTHON
The following code will display: "EN"

country = "ENGLAND"
print( country [ 0 : 2 ] )

Start position
Up to but not included

Index 0 1 2 3 4 5 6
number
Characters E N G L A N D
Substring in the exam (pseudocode)
The following code will display: " LAND "
country = "ENGLAND"
print( country.substring(3,4) )
How many letters it will
Start position display

• Substrings start from 0. So (3,4) means it starts with


the letter L “index 3”, it displays L and 3 more letters
for a total of 4 letters.
Index 0 1 2 3 4 5 6
number
Characters E N G L A N D
Substring in the exam (pseudocode)
The following code will display: " GLA "
country = "ENGLAND"
print( country.substring(2,3) )
How many letters it will
Start position display

• (2,3) means it starts with the letter G “index 2”, it


displays L and 2 more letters for a total of 3 letters.

Index 0 1 2 3 4 5 6
number
Characters E N G L A N D
Substring in the exam (pseudocode)
What will the following code display?
country = “United States"
print( country.substring (1,3) )
How many letters it will
Start position display

It will display “nit”


Index 0 1 2 3 4 5 6 7 8 9 10 11 12
number

Character U n i t e d S t a t e s
Substring in the exam (pseudocode)
What will the following code display?
country = “United States"
print( country.substring (3,3) )
How many letters it will
Start position display

It will display “ted”


Index 0 1 2 3 4 5 6 7 8 9 10 11 12
number

Character U n i t e d S t a t e s
Substring in the exam (pseudocode)
What will the following code display?
country = “United States"
print( country.substring(7,6) )
How many letters it will
Start position display

It will display “States”


Tasks
Complete task 62 – 71
Then complete any unfinished tasks from previous
lessons.
62. Create a program that:
• Ask the user for their firstname.
• Ask the user for their surname.
• Display the first letter of the user’s firstname.
• On a separate line, display the first 4 letters of the user’s firstname.
• On another line, display the last letter of the user’s surname.
Paste your code below:
63. Create a program that:
• Ask the user for a quote.
• Display the quote in uppercase
• Then displays the quote in lowercase on a separate line.
Paste your code below:
64. Create a program that:
• Ask the user for their name.
• Ask the user if they want their name to be displayed in capital letters or lowercase.
• If the user types lowercase, display the name in lowercase.
• If the user types uppercase, display the name in uppercase.
• Otherwise display “wrong choice”.
Paste your code below:
Formatting text

\n - This adds a new line. Example:


print("Hello \nWorld") would display:

\t - The t stands for “tab” which adds extra


space. Example:
print("Hello \tWorld") would display:
65) Create a program that:
• Displays “Good” then “Game” on 2 separate lines using \n and 1 print command.
• On another line, display Good game but with 4 spaces (TAB) in the middle using \t.
Paste your code below:
66. Create a program that:
• Ask the user to enter a school name.
• Ask the if they want to see the first or last letter of the school name.
• If the answer is “first”, display the first letter of the school name
• If the answer is “last”, display the last letter of the school name “hint: use -1 to display
last letter”.
• Otherwise display “wrong option”.
Paste your code below:
67. Create a program that:
• Asks the user for a song name.
• Asks the user how many characters do they want to see from the song name starting
from the first character. Example: If the user types 4, show the first 4 characters of the
song name.
• Display the correct amount of characters depending on the user’s answer.
• Store the total length of the song name in a variable called: “songLength”
• On a separate line, display the total length of the song name in a full sentence.
Paste your code below:
68. Create a program that:
• Asks the user for the name of their favourite singer.
• Store the length of the singer’s name in a variable called nameLength.
• Asks the user how many characters do they want to see from the singer’s name starting from the
first character. Example: If the user types 4, show the first 4 characters of the singer’s name.
• Add validation check so that the amount of characters the user enters is not bigger than the total
length of the name. For example: If the total length of the name is 7 characters, and the user enters
9 when asked how many characters do they want to see, then display an “Error”. Otherwise display
the correct number of characters.
Paste your code below:
69. Create a program that:
• Asks the user for their name.
• Extract the first character of their name and save it in a variable called “first”.
• Force the first character to upper case using .upper() function.
• Use concatenation to join the first letter back to the original string.
• Example: if the user types suffar as there name, you need to display: Suffar
Paste your code below:
70. Create a program that:
• Asks the user to enter a school name.
• Asks the user if they want the school name to be displayed in capital letters or lower
case.
• If the answer is “uppercase”, display the school name in uppercase.
• if the answer is “lowercase”, display the school name in lowercase
• Otherwise display “Wrong option”.
Paste your code below:
71. Identify what each of the following code will
display if quote = “Legendary” https://youtu.be/Mh79NhBK9f4
Code Output
print( quote.upper() )

print( quote.lower() )

print( len (quote) )


print( quote.substring(0) )
print( quote.substring(0,3) )
print( quote.substring(2,3) )
print( quote.substring(5,4) )
Consolidation Task: https://youtu.be/myTwo-B9E4k

You might also like