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
























