String Manipulation #
In this lab we are going to explore lots of ways to manipulate strings with Python.
Syllabus Topics [HL & SL] #
- B2.1.2 Construct programs that can extract and manipulate substrings.
- B2.3.2 Construct programs utilizing appropriate selection structures.
- B2.3.3 Construct programs that utilize looping structures to perform repeated actions.
Key Vocabulary #
| Term | Definition |
|---|---|
| Variable | A named storage location in a computer’s memory that can hold a value. The value can change during the execution of a program. |
| Initialize | The process of giving a variable its very first value when it is created. |
| Assign | To give a variable a new value after it has been initialized. |
| Data Type | A classification that specifies which type of value a variable can hold, such as a char, string, int, or a boolean (true/false) value. It determines the operations that can be performed on the data. |
| Char | A data type. Short for character, it’s a single letter, number, symbol, or space. A string is made up of multiple characters. |
| String | A data type. A sequence of characters, such as letters, numbers, and symbols, treated as a single piece of text. |
| Substring | A continous string of characters within a string |
| Slicing | The process of extracting a portion of a string using [start:stop] |
| Concatenation | Joining strings together using + |
[0] Set up #
π» Go to your
dpcs/unit00_strings folder.
cd ~/desktop/dpcs/unit00_strings/
git clone https://github.com/isf-dp-cs/lab_string_manipulation_yourgithubusername
π» In the Terminal, type the following command to open the lab folder.Below you’ll see that the
git clonecommand hasyourgithubusername.You need to replace this with your username
e.g.
https://github.com/isf-dp-cs/lab_string_manipulation_brittegenzlinger
cd lab_string_manipulation_yourgithubusername
π» Enter the Poetry Shell to start the lab. As a reminder, we will run this command at the start of each lab, but only when we are inside a lab folder.
poetry shell
πΎ π¬ Exiting the poetry shellWhen you want to exit the shell, you can type
exitor^D
[1] String Methods #
Star Tree π² #
π» In
star_tree.py, write code that prints out * in a tree pattern.
The height of the tree should be determined by user input. For example:
How tall is the tree?
>>> 4
*
***
*****
*******
How tall is the tree?
>>> 2
*
***
Abracadabra #
π» In
abracadabra.py, write code that prints out a word in a triangle pattern. The user should input the word. For example:
Tell me a word and I'll show you a trick: abracadabra
a
ra
bra
abra
dabra
adabra
cadabra
acadabra
racadabra
bracadabra
abracadabra
Tell me a word and I'll show you a trick: Huh?
?
h?
uh?
Huh?
camelCase πͺ #
When naming your variables, it can be inconvenient that you can’t use spaces. To solve this problem, programmers have come up with a few standard naming conventions:
snake_case is commonly used by Python programmers, since it’s used in Python’s built-in functions:
# snake_case
number_of_donuts = 34
camelCase is commonly used by Java, JavaScript, and TypeScript programmers:
# snake_case
numberOfDonuts = 34
There are also other less common options, such as PascalCase and kebab-case.
π» In the file
snake_to_camel.py, write code that can convert a snake_case variable into camelCase variable. For example
Variable name: number_of_donuts
numberOfDonuts
[2] ANSI Codes #
ANSI escape codes are special codes that you can concatenate onto strings to change the formatting when you print to the console.
yellow_bg = "\u001b[43;1m" # code for a yellow background
reset_code = "\u001b[0m" # code to set formatting back to normal
print(yellow_bg + "the sun is yellow" + reset)
Formatting Text #
π» Open up
ansi_test.py and complete all 4 TODO: tasks
- Add some more color codes
- Add color to the poem
- Give each letter a different color
- Print a grid of every possible background color (0-255)
Hint: the 256 Background Colors follow a simple forumula:
\u001b[48;5;+ n +m
[3] Deliverables #
β‘β¨ Once you complete the lab, be sure to complete these two steps:π Update Syllabus Tracker
These are the topics covered in this lab:
- B2.1.2 Construct programs that can extract and manipulate substrings.
- B2.3.2 Construct programs utilizing appropriate selection structures.
- B2.3.3 Construct programs that utilize looping structures to perform repeated actions.
Go to your Syllabus Content Checklist in your Google Drive and update it accordingly.
π» Push your work to Github
- git status
- git add -A
- git status
- git commit -m "describe your code here"
- git push
- git remote
[4] Extension #
Print Vertical #
π» In
print_vertical.py, write code that takes a sentence and prints it vertically. The user should input the sentence. For example:
Enter your text: how are you doing today?
haydt
orooo
weuid
na
gy
?
Enter a string: When I was a youthful lad...
WIwayl
h a oa
e s ud
n t.
h.
f.
u
l