Coconote
AI notes
AI voice & video notes
Export note
Try for free
Python String Methods Lecture
Jul 18, 2024
Python String Methods
Introduction
47 string methods in Python.
Excluding dunder methods (e.g.,
__len__
)
Common Methods
capitalize()
Capitalizes the first character.
casefold()
For caseless comparisons.
Less Common Methods
center(width, [fillchar])
Centers the string with optional
fillchar
.
count(substring)
Counts occurrences of
substring
.
encode([encoding[, errors]])
Encodes the string.
endswith(suffix[, start[, end]])
Checks if the string ends with
suffix
.
expandtabs([tabsize])
Expands tabs to spaces.
find(sub[, start[, end]])
Finds the first occurrence of
sub
.
**format(*args,
kwargs)
Formats the string.
format_map(mapping)
Uses dictionary for formatting.
Methods for Character Type Checking
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 decimals.
isdigit()
Checks if all characters are digits.
isnumeric()
Checks if all characters are numeric.
isidentifier()
Checks for a valid identifier.
islower()
Checks if all characters are lowercase.
isprintable()
Checks if all characters are printable.
isspace()
Checks if all characters are whitespace.
istitle()
Checks if the string is a title.
isupper()
Checks if all characters are uppercase.
String Manipulation Methods
join(iterable)
Joins elements of an iterable with the string acting as a separator.
ljust(width[, fillchar])
Left-justifies the string.
lower()
Converts all characters to lowercase.
lstrip([chars])
Strips leading characters.
maketrans(x[, y[, z]])
Returns a translation table.
translate(table)
Translates string using a translation table.
partition(sep)
Splits the string into a tuple (head, sep, tail).
removeprefix(prefix)
Removes the specified prefix.
removesuffix(suffix)
Removes the specified suffix.
replace(old, new[, count])
Replaces occurrences of
old
with
new
.
rfind(sub[, start[, end]])
Finds the last occurrence of
sub
.
rindex(sub[, start[, end]])
Like
rfind
but raises ValueError if
sub
is not found.
rjust(width[, fillchar])
Right-justifies the string.
rpartition(sep)
Splits into a tuple (tail, sep, head) from the right.
rsplit([sep[, maxsplit]])
Splits the string from the right.
rstrip([chars])
Strips trailing characters.
split([sep[, maxsplit]])
Splits the string from the left.
splitlines([keepends])
Splits on line breaks.
startswith(prefix[, start[, end]])
Checks if string starts with the prefix.
strip([chars])
Strips leading and trailing characters.
swapcase()
Swaps case; converts uppercase to lowercase and vice versa.
title()
Converts string to title case.
upper()
Converts all characters to uppercase.
zfill(width)
Pads string on the left with zeros.
Conclusion
Important to know various string methods for efficient string manipulation in Python.
Practice with these methods to understand their use-cases.
Reach out with questions or suggestions.
Happy New Year!
📄
Full transcript