7.4. String slices

A segment of a string is called a slice. Selecting a slice is similar to selecting a character:

The operator returns the part of the string from the “n-th” character to the “m-th” character, including the first but excluding the last.

If you omit the first index (before the colon), the slice starts at the beginning of the string. If you omit the second index, the slice goes to the end of the string:

If the first index is greater than or equal to the second the result is an empty string, represented by two quotation marks:

An empty string contains no characters and has length 0, but other than that, it is the same as any other string.

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

You have attempted 1 of 8 activities on this page