Description:
Python string methods are built-in functions that can be called on string objects to perform various operations and manipulations. These methods are accessed using dot notation (e.g., my_string.method_name()) and return new strings or Boolean values, rather than modifying the original string (as strings are immutable in Python).
Common Categories and Examples of String Methods:
Case Manipulation:
upper(): Converts all characters to uppercase.
lower(): Converts all characters to lowercase.
capitalize(): Capitalizes the first character and lowercases the rest.
title(): Capitalizes the first letter of each word.
swapcase(): Swaps the case of all characters (uppercase to lowercase and vice versa).
Whitespace and Character Removal/Replacement:
strip(): Removes leading and trailing whitespace.
lstrip(): Removes leading whitespace.
rstrip(): Removes trailing whitespace.
replace(old, new): Replaces all occurrences of a substring with another.
Searching and Checking:
find(substring): Returns the lowest index of the substring if found, otherwise -1.
index(substring): Similar to find(), but raises a ValueError if the substring is not found.
startswith(prefix): Returns True if the string starts with the specified prefix.
endswith(suffix): Returns True if the string ends with the specified suffix.
isalnum(): Checks if all characters are alphanumeric.
isalpha(): Checks if all characters are alphabetic.
isdigit(): Checks if all characters are digits.
islower(), isupper(), istitle(): Check for specific casing.
Splitting and Joining:
split(delimiter): Splits the string into a list of substrings based on a delimiter.
join(iterable): Joins elements of an iterable (e.g., a list of strings) into a single string using the string itself as a separator.
Share this link via
Or copy link























