|
⇤ ← Revision 1 as of 2023-01-10 19:53:45
Size: 3490
Comment:
|
← Revision 2 as of 2025-12-23 04:06:27 ⇥
Size: 3643
Comment: Cleanup
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 11: | Line 11: |
| == Capitalize == | == Methods == |
| Line 13: | Line 13: |
| `str.capitalize()` ---- |
||'''Method Name'''||'''Description'''||'''Example'''|| ||`capitalize` || ||`str.capitalize()`|| ||`casefold` ||Convert to lowercase||`str.casefold()`|| ||`center` || ||`str.center(width, ' ')`|| ||`count` || ||`str.count(sub, 0, len(sub)-1)`|| ||`encode` || ||`str.encode(encoding='utf-8', errors='strict')`|| ||`endswith` || ||`str.endswith(suffix0, len(sub)-1)`|| ||`expandtabs` || ||`str.expandtabs(tabsize=8)`|| ||`find` || ||`str.find(sub, 0, len(sub)-1)`|| ||`format` || ||`str.format(**kwargs)`|| ||`format_map` || ||`str.format_map(kwargs)`|| ||`index` || ||`str.index(sub, 0, len(sub)-1)`|| ||`isalnum` || ||`str.isalnum()`|| ||`isalpha` || ||`str.isalpha()`|| ||`isascii` || ||`str.isascii()`|| ||`isdecimal` || ||`str.isdecimal()`|| ||`isdigit` || ||`str.isdigit()`|| ||`isidentifier` || ||`str.isidentifier()`|| ||`islower` || ||`str.islower()`|| ||`isnumeric` || ||`str.isnumeric()`|| ||`isprintable` || ||`str.isprintable()`|| ||`isspace` || ||`str.isspace()`|| ||`istitle` || ||`str.istitle()`|| ||`isupper` || ||`str.isupper()`|| ||`join` || ||`str.join(iterable)`|| ||`ljust` || ||`str.ljust(width, ' ')`|| ||`lower` ||Convert to lowercase||`str.lower()`|| ||`lstrip` || ||`str.lstrip(line)`|| ||`maketrans` || || || ||`partition` || ||`str.partition(sep)`|| ||`removeprefix` || ||`str.removeprefix(prefix)`|| ||`removesuffix` || ||`str.removesuffix(suffix)`|| ||`replace` || ||`str.replace(old, new, 1)`|| ||`rfind` || ||`str.rfind(sub, 0, len(sub)-1)`|| ||`rindex` || ||`str.rindex(sub, 0, len(sub)-1)`|| ||`rjust` || ||`str.rjust(width[, fillchar])`|| ||`rpartition` || ||`str.rpartition(sep)`|| ||`rstrip` || ||`str.rstrip(line)`|| ||`split` || ||`str.split(sep=' ', maxsplit=1)`|| ||`splitlines` || ||`str.splitlines()`|| ||`startswith` || ||`str.startswith(prefix, 0, len(sub)-1)`|| ||`strip` || ||`str.strip(line)`|| ||`swapcase` || ||`str.swapcase()`|| ||`title` || ||`str.title()`|| ||`translate` || ||`str.translate(table)`|| ||`upper` ||Convert to uppercase||`str.upper()`|| ||`zfill` || ||`str.zfill(width)`|| |
| Line 19: | Line 63: |
| == CaseFold == | === Casefolding === |
| Line 21: | Line 65: |
| The '''`casefold`''' method casefolds the string to lowercase letters. Non-ASCII characters may be irreversibly replaced with ASCII 'equivalents'. {{{ orig = ['ß', 'á', 'Á'] [o.casefold() for o in orig] # ['ss', 'á', 'á'] }}} ---- == Center == `str.center(width[, fillchar])` ---- == Count == `str.count(sub[, start[, end]])` ---- == Encode == `str.encode(encoding='utf-8', errors='strict')` ---- == EndsWith == `str.endswith(suffix[, start[, end]])` ---- == ExpandTabs == `str.expandtabs(tabsize=8)` ---- == Find == `str.find(sub[, start[, end]])` ---- == Format == `str.format(*args, **kwargs)` ---- == Format_Map == `str.format_map(mapping)` ---- == Index == `str.index(sub[, start[, end]])` ---- == IsAlnum == `str.isalnum()` ---- == IsAlpha == `str.isalpha()` ---- == IsASCII == `str.isascii()` ---- == IsDecimal == `str.isdecimal()` ---- == IsDigit == `str.isdigit()` ---- == IsIdentifier == `str.isidentifier()` ---- == IsLower == `str.islower()` ---- == IsNumeric == `str.isnumeric()` ---- == IsPrintable == `str.isprintable()` ---- == IsSpace == `str.isspace()` ---- == IsTitle == `str.istitle()` ---- == IsUpper == `str.isupper()` ---- == Join == `str.join(iterable)` ---- == LJust == `str.ljust(width[, fillchar])` ---- == Lower == The '''`lower`''' method casefolds the string to lowercase letters. Unlike the `casefold` and `upper` methods, non-ASCII characters are preserved losslessly. {{{ orig = ['ß', 'á', 'Á'] [o.lower() for o in orig] # ['ß', 'á', 'á'] }}} ---- == LStrip == `str.lstrip([chars])` ---- == MakeTrans == `str.maketrans(x[, y[, z]])` ---- == Partition == `str.partition(sep)` ---- == RemovePrefix == `str.removeprefix(prefix, /)` ---- == RemoveSuffix == `str.removesuffix(suffix, /)` ---- == Replace == `str.replace(old, new[, count])` ---- == RFind == `str.rfind(sub[, start[, end]])` ---- == RIndex == `str.rindex(sub[, start[, end]])` ---- == RJust == `str.rjust(width[, fillchar])` ---- == RPartition == `str.rpartition(sep)` ---- == RStrip == `str.rstrip([chars])` ---- == Split == `str.split(sep=None, maxsplit=-1)` ---- == SplitLines == `str.splitlines(keepends=False)` ---- == StartsWith == `str.startswith(prefix[, start[, end]])` ---- == Strip == `str.strip([chars])` ---- == SwapCase == `str.swapcase()` ---- == Title == `str.title()` ---- == Translate == `str.translate(table)` ---- == Upper == The '''`upper`''' method casefolds the string to uppercase letters. Non-ASCII characters may be irreversibly replaced with ASCII 'equivalents'. |
The `upper`, `lower`, and `casefold` methods are closely related. The major difference is that `lower` is reversible. |
| Line 386: | Line 70: |
[o.casefold() for o in orig] # ['ss', 'á', 'á'] [o.lower() for o in orig] # ['ß', 'á', 'á'] |
|
| Line 387: | Line 75: |
---- == ZFill == `str.zfill(width)` |
Python String Methods
These methods are defined for all instances of the built-in str type.
Contents
Methods
Method Name |
Description |
Example |
capitalize |
|
str.capitalize() |
casefold |
Convert to lowercase |
str.casefold() |
center |
|
str.center(width, ' ') |
count |
|
str.count(sub, 0, len(sub)-1) |
encode |
|
str.encode(encoding='utf-8', errors='strict') |
endswith |
|
str.endswith(suffix0, len(sub)-1) |
expandtabs |
|
str.expandtabs(tabsize=8) |
find |
|
str.find(sub, 0, len(sub)-1) |
format |
|
str.format(**kwargs) |
format_map |
|
str.format_map(kwargs) |
index |
|
str.index(sub, 0, len(sub)-1) |
isalnum |
|
str.isalnum() |
isalpha |
|
str.isalpha() |
isascii |
|
str.isascii() |
isdecimal |
|
str.isdecimal() |
isdigit |
|
str.isdigit() |
isidentifier |
|
str.isidentifier() |
islower |
|
str.islower() |
isnumeric |
|
str.isnumeric() |
isprintable |
|
str.isprintable() |
isspace |
|
str.isspace() |
istitle |
|
str.istitle() |
isupper |
|
str.isupper() |
join |
|
str.join(iterable) |
ljust |
|
str.ljust(width, ' ') |
lower |
Convert to lowercase |
str.lower() |
lstrip |
|
str.lstrip(line) |
maketrans |
|
|
partition |
|
str.partition(sep) |
removeprefix |
|
str.removeprefix(prefix) |
removesuffix |
|
str.removesuffix(suffix) |
replace |
|
str.replace(old, new, 1) |
rfind |
|
str.rfind(sub, 0, len(sub)-1) |
rindex |
|
str.rindex(sub, 0, len(sub)-1) |
rjust |
|
str.rjust(width[, fillchar]) |
rpartition |
|
str.rpartition(sep) |
rstrip |
|
str.rstrip(line) |
split |
|
str.split(sep=' ', maxsplit=1) |
splitlines |
|
str.splitlines() |
startswith |
|
str.startswith(prefix, 0, len(sub)-1) |
strip |
|
str.strip(line) |
swapcase |
|
str.swapcase() |
title |
|
str.title() |
translate |
|
str.translate(table) |
upper |
Convert to uppercase |
str.upper() |
zfill |
|
str.zfill(width) |
Casefolding
The upper, lower, and casefold methods are closely related. The major difference is that lower is reversible.
orig = ['ß', 'á', 'Á'] [o.upper() for o in orig] # ['SS', 'Á', 'Á'] [o.casefold() for o in orig] # ['ss', 'á', 'á'] [o.lower() for o in orig] # ['ß', 'á', 'á']
