ISF DP Computer Science

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 #

TermDefinition
VariableA named storage location in a computer’s memory that can hold a value. The value can change during the execution of a program.
InitializeThe process of giving a variable its very first value when it is created.
AssignTo give a variable a new value after it has been initialized.
Data TypeA 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.
CharA data type. Short for character, it’s a single letter, number, symbol, or space. A string is made up of multiple characters.
StringA data type. A sequence of characters, such as letters, numbers, and symbols, treated as a single piece of text.
SubstringA continous string of characters within a string
SlicingThe process of extracting a portion of a string using [start:stop]
ConcatenationJoining strings together using +

[0] Set up #

πŸ’» Go to your dpcs/unit00_strings folder.

cd ~/desktop/dpcs/unit00_strings/
πŸ’» Clone your repo. This will copy it onto your computer.
git clone https://github.com/isf-dp-cs/lab_string_manipulation_yourgithubusername

Below you’ll see that the git clone command has yourgithubusername.

You need to replace this with your username

e.g. https://github.com/isf-dp-cs/lab_string_manipulation_brittegenzlinger

πŸ’» In the Terminal, type the following command to open the lab folder.
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 shell

When you want to exit the shell, you can type exit or ^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 #

πŸ’» 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