──────────────────────────────────────

17. Message Lab · Read a Call Sign

Use an index to find one character and a slice to take part of a string.

Lesson files · lessons/strings-and-messages/positions-and-slices

Before you start
- Read a line of text with input().
- Predict where a range() stops.

By the end
- Use an index to get one character and a slice to get part of a string.
- Use upper() on the sliced text.
- Predict what the slice gives you when the call sign contains one character.

Where you can use it
Make a short badge code from the start of a chosen name. A slice can still produce a code when the name has only one letter.

Python numbers string positions from zero. In ORBIT-42, O is at position 0, R is at 1 and B is at 2. signal[0] gives O. The slice signal[:3] takes the first three characters, stopping before position 3.

Put .upper() after a slice to make its letters uppercase. For a one-character input such as q, [:2] returns the one available character and .upper() turns it into Q.

Predict
What does signal[:3] contain?
- ORB
- ORBI

Worked example (example.py)

signal = "ORBIT-42"
print(signal[0])
print(signal[:3])


Example result
O
ORB

Read a call sign and print its first two characters in uppercase.

Starter program (starter.py)

signal = "ORBIT-42"
print(signal[0])
print(signal[:3])


Hint
Use input()[:2].upper(). Apply the slice before printing.

Case 1. Try the sample
Input
golden-1
Expected output
GO

Case 2. One-character call sign
Input
q
Expected output
Q

Stretch
Try signal[-2:] and explain what the negative start means.

Reflect
How many characters does [:2] request?

Check your prediction
ORB Positions 0, 1 and 2 are included.
