Example
String Indexing and Slicing
Accessing individual characters and text fragments. Zero-based indexing in Python.
Showing 1–10 of 11
Example
Accessing individual characters and text fragments. Zero-based indexing in Python.
Quiz
Free beginner quiz on Python strings: 15 questions on joining, indexing, slicing, len(), split(), join() and string methods, with instant answers.
Challenge
code[0] = "X" stops with a TypeError, because Python strings can't be changed. Predict the crash, find out what methods and slices really give, build a new string instead, then swap a letter in the…
Challenge
…last four characters of an access code with * using len(), slicing and string repetition, then find a code inside a log line and mask it there before the line is saved.
Challenge
Check whether a mission log line contains ERROR with in, find where it starts with find(), including the -1 it gives when there is nothing to find, then slice out the error message.
Challenge
…then put them back together in a new order with an f-string.
Challenge
A slice meant to take the number from the container tag CG-0042 gives -004. Predict it, work out where indexes start and where a slice ends, fix it, then slice a sample label yourself.
Challenge
Extend the parser from "Parse a telemetry line" to read three readings from a multi-line feed, print each as one row, then turn them into a report with a header and aligned columns.
Challenge
…at the end. A negative start counts from the end, as with strings. The window moves A new reading has arrived. Add new_reading = 772 under the list, append it to the readings, then show the new window…
Quiz
…for beginners: 20 multiple-choice questions on print(), strings, lists, dictionaries, loops and functions, with instant answers.