liveBook · Manning

String

As explained in Learn AI-Assisted Python Programming, Second Edition

A string is a sequence of characters represented as a text value in a programming language.

A string is a text value: a sequence of characters used to store and manipulate words, passwords, phone numbers, books, or digit characters. Write a specific string inside matching single or double quotation marks. section 4.3.4 section 3.3.2

Use strings to perform several practical programming tasks:

  • Call string methods with dot notation, such as isupper(), isdigit(), isalnum(), and replace(). section 4.3.4
  • Combine strings with +, which concatenates text rather than adding numbers. section 4.3.4 section 10.3.3
  • Index strings to access individual characters, including digit characters in a code that must preserve a leading zero. section 10.3.3
  • Pass strings into functions, return them, and test them with ordinary and edge-case inputs. section 6.2.1 section 3.3.3

Cleaning a phone number

A function can remove selected characters by repeatedly calling replace(): phone_number.replace('(', ''), then replacing ')' and '-'. The cleaned value remains a string. section 4.3.4

def clean_number(phone_number):
    phone_number = phone_number.replace('(', '')    #1
    phone_number = phone_number.replace(')', '')     #2
    phone_number = phone_number.replace('-', '')    #3
    return phone_number

Inspect a complete string-cleaning function that replaces parentheses and hyphens with empty strings before returning the result. section 4.3.4

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Explains quotation syntax, methods, concatenation, indexing, and testing, without descending into string implementation internals.
Examples
Uses password validation, phone-number cleaning, sentence construction, secret codes, longest-word tests, and empty strings.
What it assumes
Introduces strings from basic text values while assuming only elementary function calls, variables, and input-output testing.
What you can do after
Apply it via Python to store, inspect, combine, clean, index, validate, and test text values.
How it connects
Connects strings with functions, methods, indexing, Boolean results, lists, modules, and unit testing.

Also explained in

Compare how 24 books explain string →

Related concepts

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