Code Labs: Strings & Random

Put String Concatenation, Slicing, and Random Output into practice.

Interactive Engine

Use the interactive terminal below to test string slicing.

> Waiting to compile...

The Crucible

Prove your knowledge of string cutting and randomisation.

Bronze (Join)

The Missing Space Trap

Computers don't automatically insert spaces when you glue two pieces of text together.

Task: Use the + operator to combine firstName, a literal space string " ", and surname so the output is exactly: "Ada Lovelace".

> Output will appear here...
Silver (Slicing)

The String Surgeon

You need to generate an ID abbreviation from a school subject. The program must extract only the first 3 letters of the subject.

Task: Use array indexing `[]` to slice exactly the letters "COM" out of the variable subject. Remember: counting starts at 0!

> Output will appear here...
Gold (Random)

The Digital Dice

You need to simulate a tabletop role-playing game where a player rolls a 20-sided dice (D20). If they roll a 20, they score a Critical Hit!

Task: Import the random library, and use the random.randint() function to generate a number between exactly 1 and 20. Store it in roll. You can run the code multiple times to see different results.

> Output will appear here...