Strings in Python

Python provides a rich collection of built-in string functions and methods for manipulating and analyzing text data. These functions are essential tools for text processing in Python applications.

Basic String Functions

  • len() - Returns the number of characters in a string
  • str() - Converts other data types to string representation
  • repr() - Returns a printable representation of an object, often used for debugging

String Case Manipulation

  • upper() - Converts all characters to uppercase
  • lower() - Converts all characters to lowercase
  • capitalize() - Capitalizes the first character of the string
  • title() - Converts the first character of each word to uppercase
  • swapcase() - Swaps case of each character (lowercase becomes uppercase and vice versa)

String Searching and Validation

  • find() - Returns the lowest index of a substring, or -1 if not found
  • rfind() - Returns the highest index of a substring, searching from right
  • index() - Similar to find() but raises ValueError if substring is not found
  • count() - Returns the number of occurrences of a substring
  • startswith() - Returns True if string starts with the specified prefix
  • endswith() - Returns True if string ends with the specified suffix
  • isalpha() - Returns True if all characters are alphabetic
  • isdigit() - Returns True if all characters are digits
  • isalnum() - Returns True if all characters are alphanumeric
  • isspace() - Returns True if all characters are whitespace
  • islower() - Returns True if all characters are lowercase
  • isupper() - Returns True if all characters are uppercase
  • istitle() - Returns True if string is title-cased

String Transformation

  • replace() - Replaces occurrences of a substring with another
  • strip() - Removes leading and trailing whitespace (or specified characters)
  • lstrip() - Removes leading whitespace (or specified characters)
  • rstrip() - Removes trailing whitespace (or specified characters)
  • split() - Splits string into a list based on a delimiter
  • rsplit() - Splits string from the right
  • splitlines() - Splits string at line breaks
  • join() - Concatenates elements of an iterable with the string as separator
  • partition() - Splits the string at the first occurrence of a separator
  • rpartition() - Splits the string at the last occurrence of a separator

String Formatting

  • format() - Formats string using positional and keyword arguments
  • center() - Centers string in a field of specified width
  • ljust() - Left-aligns string in a field of specified width
  • rjust() - Right-aligns string in a field of specified width
  • zfill() - Pads string with zeros on the left
  • expandtabs() - Replaces tabs with spaces

String Encoding and Conversion

  • encode() - Returns encoded version of the string
  • decode() - Decodes the string using the specified encoding
  • maketrans() - Creates a translation table for translate()
  • translate() - Returns a translated string using a translation table

String Content Inspection

  • ord() - Returns the Unicode code point for a character
  • chr() - Returns the character for a Unicode code point

String functions in Python are method-based (called on string objects) and provide a consistent, powerful interface for text processing. Understanding these functions enables efficient text analysis, cleaning, and transformation in Python applications.