liveBook · Manning

String

As explained in Tiny Python Projects: Learn coding and testing with puzzles and games

A string is a Python value representing a sequence of text characters.

A string is a Python value representing a sequence of text characters. Python represents strings with the str class; quoted values such as 'narwhal' and "submarine" have type str. section 2.1.6

Work with strings by combining indexing, methods, operators, and tests:

  • Inspect characters with zero-based indexes such as word[0] and word[-1], or extract portions with slices such as word[1:-1]. section 2.5 section 16.1.4
  • Call methods with parentheses: word.upper() returns an uppercase string, while word.isupper() returns a Boolean; methods can be chained. section 2.1.9
  • Use len() for length, + for concatenation, str.format() or f-strings for templates, and x in y to test membership. section 2.1.9 section 2.5
  • Treat strings as immutable: create a new string with replace(), translate(), a loop, or by joining a modified list of characters. section 8.1 section 8.6
  • Use tests to check string-processing behavior, including edge cases and deterministic results when randomness is involved. section 16.1.4 section 2.5

Choose “a” or “an”

A small command-line program can inspect the first character and build a new message: word = 'OCTOPUS' article = 'an' if word[0].lower() in 'aeiou' else 'a' message = f'Ahoy, Captain, {article} {word.lower()} off the larboard bow!' print(message) Output: Ahoy, Captain, an octopus off the larboard bow! The same pattern combines indexing, lower(), membership testing, a conditional expression, and an f-string. section 2.0 section 2.1.10

Avast, you corny-faced gollumpus! Ye are barrelman for this watch. D’ye ken what I mean, ye addle-pated blunderbuss?! Ah, landlubber ye be! OK, then, you are the lookout in the crow’s nest--the little bucket attached to the top of a mast of a sailing ship. Your job is to keep a lookout for interesting or dangerous things, like a ship to plunder or an iceberg to avoid. When you see something like a narwhal, you are supposed to cry out, “Ahoy, Captain, a narwhal off the larboard bow!” If you see an octopus, you’ll shout “Ahoy, Captain, an octopus off the larboard bow!” (We’ll assume everything is “off the larboard bow” for this exercise. It’s a great place for things to be.)

Use the crow’s-nest prompt to compare the required output for a consonant-starting word with the output for a vowel-starting word. section 2.0

How Tiny Python Projects: Learn coding and testing with puzzles and games covers it

How deep it goes
Explains types, indexing, methods, immutability, formatting, membership, and testing through runnable examples, stopping short of formal string internals.
Examples
Demonstrates greeting generation, vowel detection, find-and-replace, character mutation, word scrambling, holiday verses, and numeric word encoding.
What you can do after
Build it from scratch by writing, testing, transforming, formatting, and validating Python string-processing programs.
How it connects
Connects strings with lists, slicing, regular expressions, string methods, command-line arguments, randomization, ASCII values, and test-driven development.

Also explained in

Compare how 24 books explain string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny Python Projects: Learn coding and testing with puzzles and games. The section links point to the parts of the book it draws on.