Python String Methods
Python String Methods
Overview of string manipulation functionalities in Python.
Case Conversion
Methods for altering the casing of strings for specific purposes.
capitalize()
Transforms the first character to uppercase.
casefold()
Converts the string to lowercase, for caseless matching.
lower()
Converts all characters in the string to lowercase.
swapcase()
Swaps uppercase characters to lowercase and vice versa.
upper()
Converts all characters in the string to uppercase.
Searching and Replacing
Includes methods to search or replace substrings within a string.
count()
Counts occurrences of a substring in the string.
endswith()
Checks if the string ends with the specified substring.
find()
Finds the lowest index of the specified substring.
replace()
Replaces occurrences of a substring with another substring.
startswith()
Checks if the string starts with the specified substring.
rfind()
Finds the highest index of the specified substring.
Formatting
Methods designed to assist in formatting strings.
center()
Centers the string within a specified width.
format()
Formats strings using placeholders.
ljust()
Justifies the string to the left with a specific width.
rjust()
Justifies the string to the right with a specific width.
zfill()
Pads the string with zeros on the left, to fill a width.
Trimming and Adjusting
For trimming or adjusting whitespace and characters from strings.
expandtabs()
Expands tab characters in the string into spaces.
lstrip()
Removes leading characters (spaces by default).
rstrip()
Removes trailing characters (spaces by default).
strip()
Removes both leading and trailing characters (spaces by default).
Splitting and Joining
Methods to split strings or join collections of strings.
join()
Joins the elements of an iterable into a single string.
partition()
Splits the string at the first occurrence of the separator.
rpartition()
Splits the string at the last occurrence of the separator.
split()
Splits a string into a list at each occurrence of a separator.
splitlines()
Splits a string at line breaks, returning a list.
Character Inquiry
To check properties of characters within the string.
isalnum()
Checks if all characters are alphanumeric.
isalpha()
Checks if all characters are alphabetic.
isascii()
Checks if all characters are ASCII.
isdecimal()
Checks if all characters are decimal characters.
isdigit()
Checks if all characters are digits.
isidentifier()
Checks if the string is a valid identifier.
islower()
Checks if all characters are lowercase.
isnumeric()
Checks if all characters are numeric.
isprintable()
Checks if all characters are printable.
isspace()
Checks if all characters are whitespace.
istitle()
Checks if the string is in title case.
isupper()
Checks if all characters are uppercase.
Miscellaneous
Other various operations related to strings.
encode()
Encodes the string to bytes.
maketrans()
Creates a translation table.
translate()
Translates characters using a translation table.
Method Categories Table
| Method Category | Methods | Description |
|-----------------------------|----------------------------------------------|-------------------------------------------------|
| Case Conversion | capitalize, casefold, lower, swapcase, title, upper | Altering string case |
| Searching and Replacing | count, endswith, find, index, replace, startswith, rfind, rindex | Finding or replacing substrings |
| Formatting | center, format, format_map, ljust, rjust, zfill | Formatting strings |
| Trimming and Adjusting | expandtabs, lstrip, rstrip, strip | Trimming or adjusting whitespace |
| Splitting and Joining | join, partition, rpartition, split, rsplit, splitlines | Splitting or joining strings |
| Character Inquiry | isalnum, isalpha, isascii, isdecimal, isdigit, isidentifier, islower, isnumeric, isprintable, isspace, istitle, isupper | Checking character types |
| Miscellaneous | encode, maketrans, translate | Various other operations |
String Manipulation Methods
Methods commonly used to manipulate and modify strings in programming.
Case Conversion
Transforming the case of strings.
To Uppercase
Convert all characters in a string to uppercase.
To Lowercase
Convert all characters in a string to lowercase.
Capitalize
Capitalize the first letter of a string.
Swapcase
Swap the case of each letter in a string.
Title Case
Convert a string to title case (each word capitalized).
Searching
Locating specific substrings or patterns within a string.
IndexOf
Find the first occurrence of a substring.
LastIndexOf
Find the last occurrence of a substring.
Contains
Check if the string contains a certain substring.
Starts With
Check if the string starts with a specific substring.
Ends With
Check if the string ends with a specific substring.
Replacing
Substituting parts of the string with another substring.
Replace
Replace occurrences of a substring with another substring.
ReplaceAll
Replace all occurrences based on a matched pattern (regex).
ReplaceFirst
Replace the first occurrence based on a matched pattern.
Remove
Remove specific characters or substrings from a string.
Splitting
Dividing a string into multiple parts.
Split
Divide a string into an array of substrings based on a delimiter.
Partition
Split a string into two parts based on the first occurrence of a delimiter.
SplitLines
Split a string into an array of substrings based on line breaks.
Chunk
Divide a string into an array of substrings with a specified size.
Trimming
Eliminating unwanted characters from the beginning and/or end of the string.
Trim
Remove whitespace from both ends of a string.
TrimStart
Remove whitespace from the beginning of a string.
TrimEnd
Remove whitespace from the end of a string.
Strip
Remove specific characters from both ends of a string.
Padding
Adding characters to a string to reach a certain length.
PadStart
Add characters to the beginning until a certain length is reached.
PadEnd
Add characters to the end until a certain length is reached.
PadBoth
Add characters to both ends to reach a certain length.
Formatting
Creating formatted strings by including variables or expressions.
Format
Insert values into a string with placeholders.
Interpolate
Embed expressions within a string for evaluation and substitution.
Concatenate
Join multiple strings into a single string.
String Builder
Efficiently build strings from multiple segments.
String Method Categories
Exploring different categories of string methods in programming.
Accessing
Methods to access characters and substrings.
CharAt
Returns the character at the specified index.
Substring
Extracts characters from a string between two specified indices.
Slice
Extracts a part of a string and returns it as a new string.
IndexOf
Returns the index within the calling String object of the first occurrence of the specified value.
Modification
Methods to alter string content.
Replace
Substitutes a specified portion of the string with another value.
Trim
Removes whitespace from both ends of a string.
Concat
Combines the text of two or more strings and returns a new string.
ToUpper/LowerCase
Converts a string to uppercase or lowercase.
Query
Methods to search for information within a string.
Length
Returns the length of the string.
EndsWith/StartsWith
Determines whether a string begins or ends with the characters of another string.
Includes
Determines whether one string may be found within another string.
Match
Retrieves the result of matching a string against a regular expression.
Utility
Additional string methods that serve various purposes.
Split
Divides a string into an ordered list of substrings.
Repeat
Returns a new string with a specified number of copies of the string it was called on.
CharCodeAt
Returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.
Normalize
Returns the Unicode Normalization Form of a given string.
Mindmap of Seven Categories
Understanding the structure of your request, I'll present the main node and follow with its seven category branches.
Category 1: Education
Advances the knowledge and skills of individuals in various fields.
Category 2: Technology
Encompasses digital advancements, gadgets, and the Internet of Things.
Category 3: Health
Covers physical, mental, and public health concerns and improvements.
Category 4: Environment
Focuses on ecosystem protection, sustainability, and climate change.
Category 5: Economy
Deals with finance, trade, and the overall wealth of societies.
Category 6: Society
Considers cultural, social, and ethical aspects of human communities.
Category 7: Politics
Relates to governance, policies, and the dynamics of power.