You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

8.2 String slicing

3 min readjune 24, 2024

Strings in Python are sequences of characters with powerful and capabilities. You can access individual characters using positive or , starting from 0 or -1 respectively. This allows for precise character retrieval within strings.

Slicing lets you extract substrings by specifying start, end, and step values. It's a versatile tool for manipulating strings, from basic to advanced operations like reversing. Remember, strings are immutable, so operations create new objects.

String Indexing and Slicing

String character indexing

Top images from around the web for String character indexing
Top images from around the web for String character indexing
  • Strings consist of a of individual characters
    • Each character occupies a specific position within the string called its index
    • String indexing begins at 0 for the first character and increments by 1 for each subsequent character
    • The index of the last character equals the length of the string minus 1 (
      len(string) - 1
      )
  • Access individual characters using positive indexing
    • Syntax:
      [string[index]](https://www.fiveableKeyTerm:string[index])
    • Retrieves the character at the specified index position
    • "Hello"[0] yields "H" (the character at index 0)
  • Access individual characters using negative indexing
    • Counts backwards from the end of the string
    • -1 refers to the last character, -2 the second-to-last, and so on
    • "Hello"[-1] yields "o" (the last character)

Substring extraction with slicing

  • Slicing extracts a portion of a string and returns it as a new string ( extraction)
  • Slicing syntax:
    [string[start:end:step]](https://www.fiveableKeyTerm:string[start:end:step])
    • start
      : The index at which to begin the slice (inclusive), defaults to 0 if not provided
    • end
      : The index at which to end the slice (exclusive), defaults to the length of the string if not provided
    • step
      : The or interval between characters to include, defaults to 1 if not provided
  • Slicing with positive indexes
    • "Hello"[1:4] yields "ell" (characters from index 1 up to, but not including, index 4)
    • "Hello"[1:] yields "ello" (characters from index 1 to the end of the string)
    • "Hello"[:3] yields "Hel" (characters from the beginning of the string up to, but not including, index 3)
  • Slicing with negative indexes
    • "Hello"[-4:-1] yields "ell" (characters from the fourth-to-last index up to, but not including, the last index)
    • "Hello"[:-2] yields "Hel" (characters from the beginning of the string up to, but not including, the second-to-last index)
    • "Hello"[-3:] yields "llo" (characters from the third-to-last index to the end of the string)
  • Slicing with a step
    • "Hello"[0:5:2] yields "Hlo" (every second character from index 0 to 5)
    • "Hello"[::2] yields "Hlo" (every second character from the beginning to the end of the string)
    • "Hello"[::-1] yields "olleH" (reverses the order of the characters in the string)

Immutability of strings

  • Strings are immutable data types in Python
    • Once a string is created, its contents cannot be altered
    • Operations that appear to modify a string actually return a new string object
  • Assigning a new value to a specific index position is not permitted
    • "Hello"[0] = "J" raises a
      TypeError
      because strings do not support item assignment
  • Concatenating strings using the
    +
    operator creates a new string
    • "Hello" + " World" returns a new string "Hello World"
  • Slicing a string returns a new string containing the extracted substring
    • "Hello"[1:4] returns a new string "ell"
  • To modify a string, create a new string with the desired changes
    • "Hello".replace("H", "J")
      returns a new string "Jello"

String operations and concepts

  • Strings are sequences of characters, allowing for various operations
  • Indexing is used to access individual characters within a string
  • Slicing is a technique for extracting substrings from a string
  • The stride in slicing determines the step size between characters
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary