liveBook · Manning

String

As explained in Python Workout, Second Edition

A string is an immutable sequence of characters in Python, including text assembled with formatting.

A string is Python’s text type: an immutable sequence of Unicode characters encoded in UTF-8. Words, sentences, paragraphs, and file contents can all be represented and manipulated as strings. Python has no separate character type; a single character is simply a string of length one. section 3.0

Treat strings as sequences rather than only as text. You can iterate over their characters, retrieve elements by index, search with in, and extract subsets with slices. Indexing returns one-character strings, while slicing returns another string. section 3.0 section 4.3

Use these operations to solve common text-processing tasks:

  • Search for membership with in, such as word[0] in 'aeiou', instead of repeating a long chain of comparisons. Membership search is linear in the sequence length. section 3.3
  • Split text into fields with str.split. Calling split() without an argument treats runs of spaces, tabs, and newlines as one separator; passing a string selects a specific separator. section 3.7
  • Reassemble sequence elements with str.join. Use an empty string to join characters without gaps, or a separator such as a space or comma between words. section 3.11 section 3.17
  • Sort characters with sorted, then join the resulting list to produce a string in Unicode order. section 3.15
  • Remember that strings cannot be changed in place. Rebinding a variable creates a new string reference, while repeated += operations create progressively larger string objects; accumulate pieces in a list and join them instead. section 3.5 section 3.7

Translate a sentence with string operations

A Pig Latin sentence exercise combines splitting, per-word processing, and joining. Split the input into words, translate each word, append the translated results to a list, and join that list with spaces for one-line output. For example, translating "this is a test translation" produces "histay isway away estay ranslationtay". section 3.6 section 3.7

for index, one_letter in enumerate('abcd'):
    print(f'{index}: {one_letter}')

Trace enumerate over a string to see how Python supplies each character directly while exposing its numeric index when needed. section 2.15

How Python Workout, Second Edition covers it

How deep it goes
Explains string immutability, Unicode, sequence operations, bounds, performance, and idiomatic transformations through exercises and implementation strategies.
Examples
Uses vowel detection, Pig Latin and Ubbi Dubbi translators, sentence splitting, character sorting, indexing, slicing, and file-oriented word challenges.
What it assumes
Assumes basic Python programming and develops fluency with core sequence behavior rather than teaching syntax from zero.
What you can do after
Apply strings via built-in operations and methods to build readable text-processing programs and avoid inefficient concatenation.
How it connects
Connects strings with sequences, indexing, slicing, iteration, Unicode, lists, tuples, dictionaries, sets, and regular expressions.

Also explained in

Compare how 24 books explain string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout, Second Edition. The section links point to the parts of the book it draws on.