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 bytes type

Handling text—from user input to filenames to chunks of text to be processed—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 to contain sequences of characters, which means that you can use index or slice notation:

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

One use for slice notation with strings is to chop the newline off the end of a string (usually, a line that’s just been read from a file):

x = "Goodbye\n"
x = x[:-1]
x
 
'Goodbye'

This code is just an example. You should know that Python strings have other, better methods to strip unwanted characters, but this example illustrates the usefulness of slicing.

It’s also worth noting that there is no separate character type in Python. Whether you use an index, slicing, or some other method, when you extract a single “character” from a Python string, it’s still a one-character string, with the same methods and behavior as the original string. The same is true for the empty string "".

6.2 Basic string operations

6.3 Special characters and escape sequences

6.3.1 Basic escape sequences

6.3.2 Numeric (octal and hexadecimal) and Unicode escape sequences

6.3.3 Printing vs. evaluating strings with special characters

6.4 String methods

6.4.1 The split and join string methods

6.4.2 Converting strings to numbers

6.4.3 Getting rid of extra whitespace

6.4.4 String searching

6.4.5 Modifying strings

6.4.6 Modifying strings with list manipulations

6.4.7 Useful methods and constants