Summer Deals - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Quick recap
Indexing and slicing
11. Slicing strings
Summary

Instruction

Great! In Python, you can also slice strings. Take a look:

alphabet = 'abcdefghijklmnopqrstuvwyxz'
alphabet[2:5]

The query above will return all characters with indices between 2 (inclusive) and 5 (exclusive). This means that we'll get the following:

  • The third element (c, index 2)
  • The fourth element (d, index 3)
  • The fifth element (e, index 4)

Exercise

Here is pi with 75 digits after the decimal point:

3.141592653589793238462643383279502884197169399375105820974944592307816406286

Use string slicing to print the first 10 digits after the decimal point.

Stuck? Here's a hint!

Just type: print(pi[2:12]).