Name - Grade - / 37: National 5 Computing Homework

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3
At a glance
Powered by AI
The passage discusses strings and common string handling operations in Python such as concatenation, substring extraction, changing case, and length determination.

Strings in Python can store text. Common operations include concatenation to join strings, extracting substrings, changing case, getting string length. Functions like lower(), upper(), len() are used.

The output of each program example is shown on pages 1-3.

National 5 Computing Homework Name -

CPU

Computational Thinking Grade - / 37


Feedback
Topic 3 - String Handling

All programming languages can store and manipulate text using string variables and build in functions.
Some examples of string handling, in the programming language Python are shown below:

Storing Text: productName = “Apple iPad Air 32Gb”


Output from Program
Concatenation: productName = “Apple ” + “iPad Air ” + “32Gb”
(joining text) print (productName) Apple iPad Air 32Gb

Sub-String: comment = “Most Excellent” Output from Program


(splitting text) part1 = comment[0:7] Most Ex
print (part1)
Note the above
two lines can
be combined
like this
print (comment[-5:-2])
print (comment[:3])
print (comment[-9:])
} lle
Mos
Excellent

quotation = “The answer is Forty Two” Output from Program


Lower Case smallQuotation = quotation.lower()
the answer is forty two
print (smallQuotation)
THE ANSWER IS FORTY TWO
Upper Case print (quotation.upper())

Length of String sentence = “I never could get the hang of Thursdays” Output from Program
sentencelength = len(sentence) 39
print(sentencelength)

For each of the following problems, think through the code and write down the output from each program.
The problems will get harder and harder.
Output from Program
1. productName = “The Hobbit” (1)
print (productName) The Hobbit

Output from Program


2. footballTeam = “Dunfermline” + “ Football “ + “Club” (1)
print (footballTeam) Dunfermline Football Club

Output from Program


3. dogBreed = “Labradoodle” (1)
print (dogBreed + dogBreed) LabradoodleLabradoodle

4. dogBreed = “Labradoodle” Output from Program (1)


dogAge = “Two” LabradoodleTwo
print (dogBreed + dogAge)

5. dogBreed = “Labradoodle” Output from Program (1)


dogAge = “Two” TwoLabradoodle
print (dogAge + dogBreed)
Created by Mr G Reid, PTC Technologies, Buckhaven High School, October 2013
C

N5 Homework Computational Thinking Topic 3 - String Handling

6. bookWeek = “The Colour of Magic” Output from Program (1)


print (bookWeek[0:1]) T

7. bookWeek = “The Colour of Magic” Output from Program (1)


print (bookWeek[:3]) The

8. bookWeek = “The Colour of Magic” Output from Program (1)


print (bookWeek[5:11]) Colour of

9. bookWeek = “The Colour of Magic” Output from Program (1)


print (bookWeek[-4:]) agic

10. bookWeek = “The Colour of Magic” Output from Program (1)


print (bookWeek[5:-10]) olou

11. firstName = “Walter” Output from Program (1)


surname = “White” W
print (firstName[0:1]) W
print (surname [:1])

12. firstName = “Walter” Output from Program (1)


surname = “White” r
print (firstName[-1:7]) e
print (surname [-1:])

13. firstName = “Walter” Output from Program (1)


surname = “White” WalWhi
print (firstName[0:3] + surname [0:3])

14. filmRelease = “World War Z” Output from Program (1)


tempFilm = filmRelease.lower() world war z
print (tempFilm)

15. filmRelease = “World War Z” Output from Program (1)


releaseDate = “31st OCT” WORLD WAR Z 31st oct
tempFilm = filmRelease.upper()
tempDate = releaseDate.lower()
print (tempFilm + “ ” + tempDate)

Output from Program


16. word1 = “central” (1)
word2 = “processing” C = central
word3 = “unit” P = processing
word4 = word1[0:1].upper() U = unit
word5 = word2[0:1].upper()
word6 = word3[0:1].upper()
print (word4 + “ = ” + word1)
print (word5 + “ = ” + word2)
print (word6 + “ = ” + word3)
C

N5 Homework Computational Thinking Topic 3 - String Handling

17. password = “spider man” Output from Program (1)


passwordLength = len(password) 10
print (passwordLength)

Output from Program


18. password = “Olympus” (1)
Your password is 7 characters long
passwordLength = len(password)
print (“Your password is ” + passwordLength + “ characters long”)

19. word = “Sydney” Output from Program (1)


middleLetter = len(word) / 2 d
#note that the next line removes any decimal place from the number
middleLetter = int(middleLetter)
print (word[middleLetter-1:middleLetter])

Python has many other functions used to manipulate strings. Here is one more:
Count: #This returns the number of times text is found in a given string
Output from Program
advice = “In winter, sensible people stay indoors”
print (advice.count(“in”)) 2

#Note that the output is 2 because the follow “in”s are found in the advice string:
In winter, sensible people stay indoors
The first “In” has a capital I so isn’t counted.

20. The following program uses string handling to create a simple password. Can you work out what
the password is? (4)
statement = “When Mr. Bilbo Baggins of Bag End announced”
letter1position = statement.count(“a”)
letter2position = statement.count(“e”)
letter3position = statement.count(“i”) Output from Program
letter4position = statement.count(“o”) ehhe
letter1 = statement[letter1position-1:letter1position]
letter2 = statement[letter2position-1:letter2position]
letter3 = statement[letter3position-1:letter3position]
letter4 = statement[letter4position-1:letter4position]
password = letter4 + letter2 + letter3 + letter1
print (password)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy