← Python Basics

Chapter 2

Strings

Strings hold text, and Python gives them many tools. This chapter covers counting characters with len(), picking one out by its position, counting from the end with negative numbers, slicing, string methods like upper() and split(), f-strings that drop values into text, and repeating a string by multiplying it. It also shows why you can't change one letter in place: strings never change once they're made.

11 cards

See the 11 cards
  1. What does len() print for "hello"?5
  2. What does word[0] print?P
  3. What does a negative index print?n
  4. What does the slice [0:2] print?Py
  5. What does .upper() print?HI
  6. What does this f-string print?Sam is 9
  7. What does a string times 3 print?ababab
  8. What's wrong here: changing one letter?TypeError
  9. Fill the blank to turn "a b c" into a listsplit
  10. Quotes that let a string run over several linesTriple quotes
  11. Character that escapes a quote inside a stringBackslash

Image credits

  • What does len() print for "hello"?: Brain Zap, CC0 1.0 Source
  • What does word[0] print?: Brain Zap, CC0 1.0 Source
  • What does a negative index print?: Brain Zap, CC0 1.0 Source
  • What does the slice [0:2] print?: Brain Zap, CC0 1.0 Source
  • What does .upper() print?: Brain Zap, CC0 1.0 Source
  • What does this f-string print?: Brain Zap, CC0 1.0 Source
  • What does a string times 3 print?: Brain Zap, CC0 1.0 Source
  • What's wrong here: changing one letter?: Brain Zap, CC0 1.0 Source
  • Fill the blank to turn "a b c" into a list: Brain Zap, CC0 1.0 Source