liveBook · Manning

String method

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

A string method is an operation provided by Python's string type for inspecting or producing text values.

A string method is a function associated with Python’s string type. Call it by placing the string first, followed by a dot, the method name, and parentheses for arguments. section 4.3.4

String methods support several practical text-processing tasks:

  • Check text, such as whether it is uppercase, numeric, or made only of letters and numbers. section 4.3.4
  • Convert capitalization, search text, format text, or replace specified substrings. section 4.3.4

Cleaning a phone number

A function can call replace() repeatedly to remove unwanted characters: phone_number.replace('(', ''), then replace ')' and '-'. Each call returns text with the selected substring replaced by the supplied replacement. section 4.3.4

>>> 'abc'.isupper()      #1
False                   
>>> 'Abc'.isupper()     
False                   
>>> 'ABC'.isupper()     
True                    
>>> 'abc'.isdigit()        #2
False                      #2
>>> '345bc'.isdigit()      #2
False                      #2
>>> '345'.isdigit()        #2
True                       #2

>>> 'abc6'.isalnum()         #3
True                         #3
>>> 'abc def'.isalnum()      #3
False                        #3
>>> 'abcdef#'.isalnum()      #3
False                        #3

Compare isupper(), isdigit(), and isalnum() with strings containing different capitalization, digits, spaces, and symbols. section 4.3.4

When an explanation of string methods is vague, ask specifically what each method does, what arguments it receives, and how it changes the result. section 4.4

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

How deep it goes
Defines method-call syntax and demonstrates common operations, but stops short of implementation internals.
Examples
Demonstrates case, digit, and alphanumeric checks, phone-number cleaning, and replacing a found word in text.
What it assumes
Assumes basic Python strings and functions while introducing method syntax through concrete examples.
What you can do after
Apply it via Python to test, search, format, and clean strings, then request more precise explanations of unfamiliar methods.
How it connects
Connects string methods with functions, strings, substring search, replacement, and code explanation.

Also explained in

Compare how 2 books explain string method →

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.