📝

Python String Methods Overview

Oct 6, 2025

Overview

This lecture covers three important Python string methods: isalpha(), isnumeric(), and isalnum(), explaining their purpose, syntax, and behavior with examples. A homework problem is also provided for practice.

isalpha() Method

  • isalpha() returns True if all characters in a string are alphabetic (A-Z, a-z), with no spaces or other characters.
  • Syntax: string.isalpha()
  • Example: "hello2".isalpha() returns False because '2' is not an alphabet.
  • Example: "helloIAmJustpreet".isalpha() returns True since all characters are letters.
  • Example: "hello I am justpreet".isalpha() returns False due to spaces.

isnumeric() Method

  • isnumeric() returns True if all characters in a string are numeric digits (0-9), no dots, minus signs, or other symbols allowed.
  • Syntax: string.isnumeric()
  • Example: "2332324".isnumeric() returns True.
  • Example: "3.14159".isnumeric() returns False because the dot is not numeric.
  • Example: "2/4".isnumeric() returns False as '/' is not a digit.
  • Example: "-3425".isnumeric() returns False because '-' is not a digit.

isalnum() Method

  • isalnum() returns True if all characters in a string are either alphabetic or numeric (no spaces or special characters).
  • Syntax: string.isalnum()
  • Example: "justpreet20".isalnum() returns True (letters and digits only).
  • Example: "22324242".isalnum() returns True (digits only).
  • Example: "JustBreathe".isalnum() returns True (letters only).
  • Example: "Just preet".isalnum() returns False due to the space.

Key Terms & Definitions

  • Alphabetic characters — Letters only (A-Z, a-z), no spaces or numbers.
  • Numeric characters — Digits only (0-9), no signs, decimals, or spaces.
  • Alphanumeric characters — Combination of letters and numbers, no spaces or special characters.

Action Items / Next Steps

  • Solve the homework: Determine and write the output for each given line of code using the isalpha(), isnumeric(), and isalnum() methods before checking in Python.