O'Reilly logo

The Quick Python Book by Naomi R. Ceder

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 6. Strings

This chapter covers

  • Understanding strings as sequences of characters
  • Using basic string operations
  • Inserting special characters and escape sequences
  • Converting from objects to strings
  • Formatting strings
  • Using the byte type

Handling text—from user input, to filenames, to processing chunks of text—is a common chore in programming. Python comes with powerful tools to handle and format text. This chapter discusses the standard string and string-related operations in Python.

6.1. Strings as sequences of characters

For the purposes of extracting characters and substrings, strings can be considered sequences of characters, which means you can use index or slice notation:

>>> x = "Hello">>> x[0]'H'>>> x[-1]'o'>>> x[1:]'ello'

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required