liveBook · Manning

String module

As explained in Python How-To

string is a Python standard-library module providing constants and utilities for common string character sets and operations.

Use Python’s string tools to validate character content, convert textual data, and reshape strings for application workflows. String methods return Boolean checks for alphanumeric, alphabetic, and numeric content, while conversion techniques recover numbers or structured values represented as text. section 2.2.1 section 2.2.3

Choose the operation that matches the string-processing task:

  • Use isalnum for usernames containing only letters and digits, isalpha for letters only, and isnumeric for nonempty strings containing numeric characters. section 2.2.1
  • Treat isnumeric carefully: decimal points, negative signs, and empty strings make the check return False. section 2.2.1
  • Use split to turn delimited text into multiple strings, and join to combine strings into one formatted value. section 2.3
  • Prefer readable concatenation, f-strings, or join according to the number and organization of the strings being combined. section 2.3.4
  • Use eval to recover lists, tuples, or dictionaries represented by text only when the data source is trusted; malformed text can raise SyntaxError. section 2.2.3

Convert and validate user data

Input arrives as a string, so inspect or convert it before comparing or processing it. For example, an age read with input must become an integer before comparison; a username can be checked with isalnum; and a value such as "[1, 2]" can be evaluated into a list only when trusted. section 2.2 section 2.2.1 section 2.2.3

bad_username0 = "123!@#"
assert bad_username0.isalnum() == False
 
bad_username1 = "abc..."
assert bad_username1.isalnum() == False
 
good_username = "1a2b3c"
assert good_username.isalnum() == True

Inspect representative username checks showing that punctuation fails isalnum while a mixed letter-and-digit value passes. section 2.2.1

How Python How-To covers it

How deep it goes
Explains validation methods, conversion pitfalls, and formatting choices through examples, while deferring deeper split and rsplit details.
Examples
Uses age input, username checks, numeric strings, structured text, fruit lists, and visited-country lists.
What it assumes
Assumes basic Python values and introduces the distinction between strings, integers, lists, tuples, dictionaries, and iterables.
What you can do after
Apply string methods and built-ins to validate text, convert represented values, and split or join application data.
How it connects
Connects strings with input, type conversion, iterables, eval, split, join, concatenation, and f-strings.

Also explained in

Compare how 2 books explain string module →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.