Skip to the workspace
Pebellslearn by building

Python syntax & reference

Find the form you need, compare how it behaves, and open the complete explanation when you need more depth.

Python 3.10+ syntax; individual entries identify newer features. Browser exercises use the configured Python runner.

4 matching syntax entries

Indexing a string

Learn in depth →

An index selects one Unicode code point from a string. Negative indices count from the end.

text[index]; text[-1]

Example

s = "CANTEEN"
print(s[0], s[-1], len(s))

Result

C N 7

Watch for: An invalid single index raises IndexError. A user-perceived character can contain multiple code points.

Practise this topic →

Slices and omitted bounds

Learn in depth →

A slice selects positions from start toward stop, excluding stop, at the chosen step.

Try different slice boundaries

Choose the boundaries. Follow the selected positions.

A slice visits positions toward its stop boundary. The stop position is excluded; reversing the step changes the direction.

0C-7
1A-6
2N-5
3T-4
4E-3
5E-2
6N-1

s[1:4]→"ANT"

Interactive model of Python slicing over Unicode code points. A visible character may contain several code points. Blank bounds are omitted, which differs from explicitly using -1 with a negative step.

text[start:stop:step]

Example

s = "CANTEEN"
print(s[1:4], s[-3:], s[::2], s[::-1])
print(repr(s[99:]))

Result

ANT EEN CNEN NEETNAC
''

Watch for: Out-of-range bounds are clipped. Step zero raises ValueError. stop-start is not a general slice-length formula.

Practise this topic →

Cleaning and splitting text

Learn in depth →

String methods return results; they do not modify the original string.

text.strip(); text.split(sep); separator.join(parts)

Example

raw = "  Asha,Rao  "
parts = raw.strip().split(",")
print(" ".join(parts))
print(repr(raw))

Result

Asha Rao
'  Asha,Rao  '

Watch for: strip(chars) removes any listed characters from both ends, not an exact prefix. Use removeprefix for a prefix.

Practise this topic →

Formatting values in text

Learn in depth →

An f-string evaluates expressions and formats the resulting values.

f"{expression:format_spec}"

Example

price = 12.5
print(f"Total: {price:.2f}")
print(f"{7:04d}")

Result

Total: 12.50
0007

Watch for: Formatting creates text. It does not change the numeric value or solve decimal arithmetic precision.

Practise this topic →
Complete explanation index (91 lessons indexed)

Working with Strings

Technical sources and review date

Original Pebells examples. Reviewed 2026-10-28.

Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.