Python foundations · 8 min

Work with text

Slice, clean, and format strings.

Strings are ordered sequences of characters. Their first character is at index 0. A slice such as word[:3] takes the first three characters. The stop index is excluded from the result.

String methods such as .strip() and .lower() return new strings. Use an f-string to insert values into a message. Start it with f, then put each value or expression inside braces.

Follow the example

Spaces are part of the original string.

Try it yourself

Change the raw string to " Academy " and use its first four characters for the uppercase code.

Target output

Code: ACAD
Show a hint

Use [:4] to stop just before index 4.

Output

Ready

Run your code to see the output here.

The first run downloads Python. Code runs in your browser. Progress and drafts are stored on this device.

Show one solution
word = "  Academy  ".strip()
code = word[:4].upper()
print(f"Code: {code}")