Copyright (c) Hyperion Entertainment and contributors.
Difference between revisions of "AmigaOS Manual: Python Methods"
(Listed dictionary methods) |
m (→.strip()) |
||
(108 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= Built-in Methods for Strings = |
= Built-in Methods for Strings = |
||
− | == capitalize() == |
+ | == .capitalize() == |
+ | |||
− | == center() == |
||
+ | <nowiki><stringobject>.capitalize()</nowiki> |
||
− | == count() == |
||
+ | |||
− | == decode() == |
||
+ | Returns a string where the first character is uppercase. For example: |
||
− | == encode() == |
||
+ | <syntaxhighlight lang="python"> |
||
− | == endswith() == |
||
+ | text = "python for everybody." |
||
− | == expandtabs() == |
||
+ | txt = text.capitalize() |
||
− | == find() == |
||
+ | print( txt ) |
||
− | == index() == |
||
+ | </syntaxhighlight> |
||
− | == isalnum() == |
||
+ | |||
− | == isalpha() == |
||
+ | Output: |
||
− | == isdecimal() == |
||
+ | Python for everybody. |
||
− | == isdigit() == |
||
+ | |||
− | == islower() == |
||
− | == |
+ | == .center() == |
+ | |||
− | == isspace() == |
||
+ | <nowiki><stringobject>.center(<length>[, <fillchar>])</nowiki> |
||
− | == istitle() == |
||
+ | |||
− | == isupper() == |
||
+ | Returns a string that is aligned to the center using character <fillchar>. The returned string is <length> characters long. If the specified length is less than the <stringobject> string's length, the original <stringobject> will be returned. If you omit the fill character, space will be used as a filler. |
||
− | == join() == |
||
+ | |||
− | == len() == |
||
+ | For example: |
||
− | == ljust() == |
||
+ | <syntaxhighlight lang="python"> |
||
− | == lower() == |
||
+ | text = "The Core of the Sun" |
||
− | == lstrip() == |
||
+ | txt = text.center( 25, '-' ) |
||
− | == maketrans() == |
||
+ | print( txt ) |
||
− | == max() == |
||
+ | </syntaxhighlight> |
||
− | == min() == |
||
+ | |||
− | == replace() == |
||
+ | Output: |
||
− | == rfind() == |
||
+ | ---The Core of the Sun--- |
||
− | == rindex() == |
||
+ | |||
− | == rjust() == |
||
− | == |
+ | == .count() == |
+ | |||
− | == split() == |
||
+ | <nowiki><stringobject>.count(<substring>[, <start>][, <end>])</nowiki> |
||
− | == splitlines() == |
||
+ | |||
− | == startswith() == |
||
+ | Returns the number of times the supplied string <substring> appears in the <stringobject> string. By default .count() searches the <substring> from the beginning of the <stringobject> string to the end of the string. |
||
− | == strip() == |
||
+ | |||
− | == swapcase() == |
||
+ | You can change the search range with the start and end parameters. The start parameter specifies the string position where the search range should start (0 is the start of the string) and the end parameter the string position where the search should end. |
||
− | == title() == |
||
+ | |||
− | == translate() == |
||
+ | For example: |
||
− | == upper() == |
||
+ | <syntaxhighlight lang="python"> |
||
− | == zfill() == |
||
+ | text = "I love AmigaOS. AmigaOS is my favourite operating system." |
||
+ | times = text.count( "AmigaOS" ) |
||
+ | print( times ) |
||
+ | </syntaxhighlight> |
||
+ | |||
+ | Output: |
||
+ | 2 |
||
+ | |||
+ | == .decode() == |
||
+ | |||
+ | <nowiki><stringobject>.decode([encoding=<encoding>][, errors=<errors>])</nowiki> |
||
+ | |||
+ | Decodes an encoded <stringobject> string and returns the result to the caller. The optional encoding parameter specifies the codec to use and the optional errors parameter the error handling method. If the encoding parameter is omitted, codec UTF-8 will be used. |
||
+ | |||
+ | The full list of available codecs is as follows: |
||
+ | {| class="wikitable" |
||
+ | ! style="text-align:left;" | Codec |
||
+ | ! style="text-align:left;" | Aliases |
||
+ | ! style="text-align:left;" | Languages |
||
+ | |- |
||
+ | | ascii || 646, us-ascii || English |
||
+ | |- |
||
+ | | big5 || big5-tw, csbig5 || Traditional Chinese |
||
+ | |- |
||
+ | | big5hkscs || big5-hkscs, hkscs || Traditional Chinese |
||
+ | |- |
||
+ | | cp037 || IBM037, IBM039 || English |
||
+ | |- |
||
+ | | cp424 || EBCDIC-CP-HE, IBM424 || Hebrew |
||
+ | |- |
||
+ | | cp437 || 437, IBM437 || English |
||
+ | |- |
||
+ | | cp500 || EBCDIC-CP-BE, EBCDIC-CP-CH, IBM500 || Western Europe |
||
+ | |- |
||
+ | | cp720 || || Arabic |
||
+ | |- |
||
+ | | cp737 || || Greek |
||
+ | |- |
||
+ | | cp775 || IBM775 || Baltic languages |
||
+ | |- |
||
+ | | cp850 || 850, IBM850 || Western Europe |
||
+ | |- |
||
+ | | cp852 || 852, IBM852 || Central and Eastern Europe |
||
+ | |- |
||
+ | | cp855 || 855, IBM855 || Bulgarian, Byelorussian, Macedonian, Russian, Serbian |
||
+ | |- |
||
+ | | cp856 || || Hebrew |
||
+ | |- |
||
+ | | cp857 || 857, IBM857 || Turkish |
||
+ | |- |
||
+ | | cp858 || 858, IBM858 || Western Europe |
||
+ | |- |
||
+ | | cp860 || 860, IBM860 || Portuguese |
||
+ | |- |
||
+ | | cp861 || 861, CP-IS, IBM861 || Icelandic |
||
+ | |- |
||
+ | | cp862 || 862, IBM862 || Hebrew |
||
+ | |- |
||
+ | | cp863 || 863, IBM863 || Canadian |
||
+ | |- |
||
+ | | cp864 || IBM864 || Arabic |
||
+ | |- |
||
+ | | cp865 || 865, IBM865 || Danish, Norwegian |
||
+ | |- |
||
+ | | cp866 || 866, IBM866 || Russian |
||
+ | |- |
||
+ | | cp869 || 869, CP-GR, IBM869 || Greek |
||
+ | |- |
||
+ | | cp874 || || Thai |
||
+ | |- |
||
+ | | cp875 || || Greek |
||
+ | |- |
||
+ | | cp932 || 932, ms932, mskanji, ms-kanji || Japanese |
||
+ | |- |
||
+ | | cp949 || 949, ms949, uhc || Korean |
||
+ | |- |
||
+ | | cp950 || 950, ms950 || Traditional Chinese |
||
+ | |- |
||
+ | | cp1006 || || Urdu |
||
+ | |- |
||
+ | | cp1026 || ibm1026 || Turkish |
||
+ | |- |
||
+ | | cp1140 || ibm1140 || Western Europe |
||
+ | |- |
||
+ | | cp1250 || windows-1250 || Central and Eastern Europe |
||
+ | |- |
||
+ | | cp1251 || windows-1251 || Bulgarian, Byelorussian, Macedonian, Russian, Serbian |
||
+ | |- |
||
+ | | cp1252 || windows-1252 || Western Europe |
||
+ | |- |
||
+ | | cp1253 || windows-1253 || Greek |
||
+ | |- |
||
+ | | cp1254 || windows-1254 || Turkish |
||
+ | |- |
||
+ | | cp1255 || windows-1255 || Hebrew |
||
+ | |- |
||
+ | | cp1256 || windows-1256 || Arabic |
||
+ | |- |
||
+ | | cp1257 || windows-1257 || Baltic languages |
||
+ | |- |
||
+ | | cp1258 || windows-1258 || Vietnamese |
||
+ | |- |
||
+ | | euc_jp || eucjp, ujis, u-jis || Japanese |
||
+ | |- |
||
+ | | euc_jis_2004 || jisx0213, eucjis2004 || Japanese |
||
+ | |- |
||
+ | | euc_jisx0213 || eucjisx0213 || Japanese |
||
+ | |- |
||
+ | | euc_kr || euckr, korean, ksc5601, ks_c-5601, ks_c-5601-1987, ksx1001, ks_x-1001 || Korean |
||
+ | |- |
||
+ | | gb2312 || chinese, csiso58gb231280, euc- cn, euccn, eucgb2312-cn, gb2312-1980, gb2312-80, iso- ir-58 || Simplified Chinese |
||
+ | |- |
||
+ | | gbk || 936, cp936, ms936 || Unified Chinese |
||
+ | |- |
||
+ | | gb18030 || gb18030-2000 || Unified Chinese |
||
+ | |- |
||
+ | | hz || hzgb, hz-gb, hz-gb-2312 || Simplified Chinese |
||
+ | |- |
||
+ | | iso2022_jp || csiso2022jp, iso2022jp, iso-2022-jp || Japanese |
||
+ | |- |
||
+ | | iso2022_jp_1 || iso2022jp-1, iso-2022-jp-1 || Japanese |
||
+ | |- |
||
+ | | iso2022_jp_2 || iso2022jp-2, iso-2022-jp-2 || Japanese, Korean, Simplified Chinese, Western Europe, Greek |
||
+ | |- |
||
+ | | iso2022_jp_2004 || iso2022jp-2004, iso-2022-jp-2004 || Japanese |
||
+ | |- |
||
+ | | iso2022_jp_3 || iso2022jp-3, iso-2022-jp-3 || Japanese |
||
+ | |- |
||
+ | | iso2022_jp_ext || iso2022jp-ext, iso-2022-jp-ext || Japanese |
||
+ | |- |
||
+ | | iso2022_kr || csiso2022kr, iso2022kr, iso-2022-kr || Korean |
||
+ | |- |
||
+ | | latin_1 || iso-8859-1, iso8859-1, 8859, cp819, latin, latin1, L1 || West Europe |
||
+ | |- |
||
+ | | iso8859_2 || iso-8859-2, latin2, L2 || Central and Eastern Europe |
||
+ | |- |
||
+ | | iso8859_3 || iso-8859-3, latin3, L3 || Esperanto, Maltese |
||
+ | |- |
||
+ | | iso8859_4 || iso-8859-4, latin4, L4 || Baltic languages |
||
+ | |- |
||
+ | | iso8859_5 || iso-8859-5, cyrillic || Bulgarian, Byelorussian, Macedonian, Russian, Serbian |
||
+ | |- |
||
+ | | iso8859_6 || iso-8859-6, arabic || Arabic |
||
+ | |- |
||
+ | | iso8859_7 || iso-8859-7, greek, greek8 || Greek |
||
+ | |- |
||
+ | | iso8859_8 || iso-8859-8, hebrew || Hebrew |
||
+ | |- |
||
+ | | iso8859_9 || iso-8859-9, latin5, L5 || Turkish |
||
+ | |- |
||
+ | | iso8859_10 || iso-8859-10, latin6, L6 || Nordic languages |
||
+ | |- |
||
+ | | iso8859_11 || iso-8859-11, thai || Thai languages |
||
+ | |- |
||
+ | | iso8859_13 || iso-8859-13, latin7, L7 || Baltic languages |
||
+ | |- |
||
+ | | iso8859_14 || iso-8859-14, latin8, L8 || Celtic languages |
||
+ | |- |
||
+ | | iso8859_15 || iso-8859-15, latin9, L9 || Western Europe |
||
+ | |- |
||
+ | | iso8859_16 || iso-8859-16, latin10, L10 || South-Eastern Europe |
||
+ | |- |
||
+ | | johab || cp1361, ms1361 || Korean |
||
+ | |- |
||
+ | | koi8_r || || Russian |
||
+ | |- |
||
+ | | koi8_u || || Ukrainian |
||
+ | |- |
||
+ | | mac_cyrillic || maccyrillic || Bulgarian, Byelorussian, Macedonian, Russian, Serbian |
||
+ | |- |
||
+ | | mac_greek || macgreek || Greek |
||
+ | |- |
||
+ | | mac_iceland || maciceland || Icelandic |
||
+ | |- |
||
+ | | mac_latin2 || maclatin2, maccentraleurope || Central and Eastern Europe |
||
+ | |- |
||
+ | | mac_roman || macroman || Western Europe |
||
+ | |- |
||
+ | | mac_turkish || macturkish || Turkish |
||
+ | |- |
||
+ | | ptcp154 || csptcp154, pt154, cp154, cyrillic-asian || Kazakh |
||
+ | |- |
||
+ | | shift_jis || csshiftjis, shiftjis, sjis, s_jis || Japanese |
||
+ | |- |
||
+ | | shift_jis_2004 || shiftjis2004, sjis_2004, sjis2004 || Japanese |
||
+ | |- |
||
+ | | shift_jisx0213 || shiftjisx0213, sjisx0213, s_jisx0213 || Japanese |
||
+ | |- |
||
+ | | utf_32 || U32, utf32 || All languages |
||
+ | |- |
||
+ | | utf_32_be || UTF-32BE || All languages |
||
+ | |- |
||
+ | | utf_32_le || UTF-32LE || All languages |
||
+ | |- |
||
+ | | utf_16 || U16, utf16 || All languages |
||
+ | |- |
||
+ | | utf_16_be || UTF-16BE || All languages (BMP only) |
||
+ | |- |
||
+ | | utf_16_le || UTF-16LE || All languages (BMP only) |
||
+ | |- |
||
+ | | utf_7 || U7, unicode-1-1-utf-7 || All languages |
||
+ | |- |
||
+ | | utf_8 || U8, UTF, utf8 || All languages |
||
+ | |- |
||
+ | | utf_8_sig || || All languages |
||
+ | |} |
||
+ | |||
+ | The errors parameter can have one of the following values: |
||
+ | {| class="wikitable" |
||
+ | ! style="text-align:left;" | Value |
||
+ | ! style="text-align:left;" | Description |
||
+ | |- |
||
+ | | backslashreplace || Uses a backslash instead of the character that could not be decoded. |
||
+ | |- |
||
+ | | ignore || Ignores the characters that cannot be decoded. |
||
+ | |- |
||
+ | | namereplace || Replaces the character with a text explaining the character. |
||
+ | |- |
||
+ | | strict || Raises an error on failure. The default value. |
||
+ | |- |
||
+ | | replace || Replaces the character with a questionmark. |
||
+ | |- |
||
+ | | xmlcharrefreplace || Replaces the character with an XML character. |
||
+ | |} |
||
+ | |||
+ | ; See also |
||
+ | * [[AmigaOS_Manual:_Python_Methods#.encode()|.encode()]] |
||
+ | |||
+ | == .encode() == |
||
+ | |||
+ | <nowiki><stringobject>.encode([encoding=<encoding>][, errors=<errors>])</nowiki> |
||
+ | |||
+ | This method encodes the string <stringobject> and returns the result to the caller. The optional encoding parameter specifies the encoding codec to be used. See [[AmigaOS_Manual:_Python_Methods#.decode()|.decode()]] for available codecs. If the codec is omitted, UTF-8 will be used. |
||
+ | |||
+ | The optional errors parameter allows you to change the handling scheme. See [[AmigaOS_Manual:_Python_Methods#.decode()|.decode()]] for the legal values. |
||
+ | |||
+ | ; See also |
||
+ | * [[AmigaOS_Manual:_Python_Methods#.decode()|.decode()]] |
||
+ | |||
+ | == .endswith() == |
||
+ | |||
+ | <nowiki><stringobject>.endswith(<suffix>[, <start>[, <end>]])</nowiki> |
||
+ | |||
+ | The endswith() method returns True if the string <stringobject> ends with the supplied string <suffix>. Otherwise it returns False. By default the method searches the string <stringobject> from start to end. You can change the search range with the start and end parameters. The start parameter specifies the string position where the search range should start (0 is the start of the string) and the end parameter the string position where the search should end. |
||
+ | |||
+ | For example: |
||
+ | <syntaxhighlight lang="python"> |
||
+ | text = "This is the house that Jack built." |
||
+ | print( text.endswith( "." ) ) |
||
+ | </syntaxhighlight> |
||
+ | |||
+ | Output: |
||
+ | True |
||
+ | |||
+ | == .expandtabs() == |
||
+ | |||
+ | <nowiki><stringobject>.expandtabs(<tabsize>)</nowiki> |
||
+ | |||
+ | == .find() == |
||
+ | |||
+ | <nowiki><stringobject>.find(<string>, <start>, <end>)</nowiki> |
||
+ | |||
+ | == .index() == |
||
+ | |||
+ | <nowiki><stringobject>.index(<string>, <start>, <end>)</nowiki> |
||
+ | |||
+ | == .isalnum() == |
||
+ | |||
+ | <nowiki><stringobject>.isalnum()</nowiki> |
||
+ | |||
+ | == .isalpha() == |
||
+ | |||
+ | <nowiki><stringobject>.isalpha()</nowiki> |
||
+ | |||
+ | == .isdecimal() == |
||
+ | |||
+ | <nowiki><stringobject>.isdecimal()</nowiki> |
||
+ | |||
+ | == .isdigit() == |
||
+ | |||
+ | <nowiki><stringobject>.isdigit()</nowiki> |
||
+ | |||
+ | Returns true if the string <stringobject> contains only digits. |
||
+ | |||
+ | == .islower() == |
||
+ | |||
+ | <nowiki><stringobject>.islower()</nowiki> |
||
+ | |||
+ | == .isnumeric() == |
||
+ | |||
+ | <nowiki><stringobject>.isnumeric()</nowiki> |
||
+ | |||
+ | == .isspace() == |
||
+ | |||
+ | <nowiki><stringobject>.isspace()</nowiki> |
||
+ | |||
+ | == .istitle() == |
||
+ | |||
+ | <nowiki><stringobject>.istitle()</nowiki> |
||
+ | |||
+ | == .isupper() == |
||
+ | |||
+ | <nowiki><stringobject>.isupper()</nowiki> |
||
+ | |||
+ | == .join() == |
||
+ | |||
+ | <nowiki><stringobject>.join(sequence)</nowiki> |
||
+ | |||
+ | == .len() == |
||
+ | |||
+ | <nowiki><stringobject>.len()</nowiki> |
||
+ | |||
+ | Returns the length of the string <stringobject>. |
||
+ | |||
+ | == .ljust() == |
||
+ | |||
+ | <nowiki><stringobject>.ljust(<width>[, <fillchar>])</nowiki> |
||
+ | |||
+ | == .lower() == |
||
+ | |||
+ | <nowiki><stringobject>.lower()</nowiki> |
||
+ | |||
+ | == .lstrip() == |
||
+ | |||
+ | <nowiki><stringobject>.lstrip()</nowiki> |
||
+ | |||
+ | == .maketrans() == |
||
+ | |||
+ | <nowiki><stringobject>.maketrans()</nowiki> |
||
+ | |||
+ | == .max() == |
||
+ | |||
+ | <nowiki><stringobject>.max(string)</nowiki> |
||
+ | |||
+ | == .min() == |
||
+ | |||
+ | <nowiki><stringobject>.min(<string>)</nowiki> |
||
+ | |||
+ | == .replace() == |
||
+ | |||
+ | <nowiki><stringobject>.replace(<old>, <new>[, <max>])</nowiki> |
||
+ | |||
+ | == .rfind() == |
||
+ | |||
+ | <nowiki><stringobject>.rfind(<string>, <start>, <end>)</nowiki> |
||
+ | |||
+ | == .rindex() == |
||
+ | |||
+ | <nowiki><stringobject>.rindex(<string>, <start>, <end>)</nowiki> |
||
+ | |||
+ | == .rjust() == |
||
+ | |||
+ | <nowiki><stringobject>.rjust(<width>[, <fillchar>])</nowiki> |
||
+ | |||
+ | == .rstrip() == |
||
+ | |||
+ | <nowiki><stringobject>.rstrip()</nowiki> |
||
+ | |||
+ | == .split() == |
||
+ | |||
+ | <nowiki><stringobject>.split(<delimiter>, <splits>)</nowiki> |
||
+ | |||
+ | == .splitlines() == |
||
+ | |||
+ | <nowiki><stringobject>.splitlines([<keeplinebreaks>])</nowiki> |
||
+ | |||
+ | Splits the string <stringobject> into a list. The splitting is done at line breaks. The optional parameter <keeplinebreaks> specifies if the line breaks should be included (True), or not (False). Default value is False |
||
+ | |||
+ | == .startswith() == |
||
+ | |||
+ | <nowiki><stringobject>.startswith(<string>, <start>, <end>)</nowiki> |
||
+ | |||
+ | == .strip() == |
||
+ | |||
+ | <nowiki><stringobject>.strip([<characters>])</nowiki> |
||
+ | |||
+ | Returns a copy of the string <stringobject> where leading and trailing characters are removed. By default the method removes all whitespace characters, but this can be changed with the optional <characters> parameter. The <characters> parameter lists a set of characters which will be removed from the beginning and end of the string. |
||
+ | |||
+ | ; Example 1: |
||
+ | Remove all leading and trailing whitespaces from string '''text''': |
||
+ | |||
+ | <syntaxhighlight lang="python"> |
||
+ | text = " 25th Century " |
||
+ | strippedText = text.strip() |
||
+ | print( '"'" + strippedText + '"' ) |
||
+ | </syntaxhighlight> |
||
+ | |||
+ | Output: |
||
+ | "25th Century" |
||
+ | |||
+ | ; Example 2: |
||
+ | Remove all of the following characters from the beginning and end of string '''text''': space, "-", "=", "*", "(", and ")": |
||
+ | |||
+ | <syntaxhighlight lang="python"> |
||
+ | text = "--= (*) =-- 25th Century ---= (*) =--" |
||
+ | strippedText = text.strip( " -=*()" ) |
||
+ | print( '"'" + strippedText + '"' ) |
||
+ | </syntaxhighlight> |
||
+ | |||
+ | Output: |
||
+ | "25th Century" |
||
+ | |||
+ | == .swapcase() == |
||
+ | |||
+ | <nowiki><stringobject>.swapcase()</nowiki> |
||
+ | |||
+ | == .title() == |
||
+ | |||
+ | <nowiki><stringobject>.title()</nowiki> |
||
+ | |||
+ | == .translate() == |
||
+ | |||
+ | <nowiki><stringobject>.translate(<translationtable>[, <deletechars>])</nowiki> |
||
+ | |||
+ | == .upper() == |
||
+ | |||
+ | <nowiki><stringobject>.upper()</nowiki> |
||
+ | |||
+ | == .zfill() == |
||
+ | |||
+ | <nowiki>stringobject.zfill(width)</nowiki> |
||
= Built-in Methods for Lists = |
= Built-in Methods for Lists = |
||
− | == append() == |
+ | == .append() == |
− | == count() == |
+ | == .count() == |
− | == extend() == |
+ | == .extend() == |
− | == index() == |
+ | == .index() == |
− | == insert() == |
+ | == .insert() == |
− | == pop() == |
+ | == .pop() == |
− | == remove() == |
+ | == .remove() == |
− | == reverse() == |
+ | == .reverse() == |
− | == sort() == |
+ | == .sort() == |
+ | |||
+ | = Built-in Methods for Sets = |
||
+ | |||
+ | == .add() == |
||
+ | == .clear() == |
||
+ | == .copy() == |
||
+ | == .difference() == |
||
+ | == .difference_update() == |
||
+ | == .discard() == |
||
+ | == .intersection() == |
||
+ | == .intersection_update() == |
||
+ | == .isdisjoint() == |
||
+ | == .issubset() == |
||
+ | == .pop() == |
||
+ | == .remove() == |
||
+ | == .symmetric_difference() == |
||
+ | == .symmetric_difference_update() == |
||
+ | == .union() == |
||
+ | == .update() == |
||
= Built-in Methods for Dictionaries = |
= Built-in Methods for Dictionaries = |
||
− | == clear() == |
+ | == .clear() == |
− | == copy() == |
+ | == .copy() == |
− | == fromkeys() == |
+ | == .fromkeys() == |
− | == get() == |
+ | == .get() == |
− | == has_key() == |
+ | == .has_key() == |
− | == items() == |
+ | == .items() == |
− | == keys() == |
+ | == .keys() == |
− | == setdefault() == |
+ | == .setdefault() == |
− | == update() == |
+ | == .update() == |
− | == values() == |
+ | == .values() == |
Latest revision as of 08:36, 18 February 2021
Contents
- 1 Built-in Methods for Strings
- 1.1 .capitalize()
- 1.2 .center()
- 1.3 .count()
- 1.4 .decode()
- 1.5 .encode()
- 1.6 .endswith()
- 1.7 .expandtabs()
- 1.8 .find()
- 1.9 .index()
- 1.10 .isalnum()
- 1.11 .isalpha()
- 1.12 .isdecimal()
- 1.13 .isdigit()
- 1.14 .islower()
- 1.15 .isnumeric()
- 1.16 .isspace()
- 1.17 .istitle()
- 1.18 .isupper()
- 1.19 .join()
- 1.20 .len()
- 1.21 .ljust()
- 1.22 .lower()
- 1.23 .lstrip()
- 1.24 .maketrans()
- 1.25 .max()
- 1.26 .min()
- 1.27 .replace()
- 1.28 .rfind()
- 1.29 .rindex()
- 1.30 .rjust()
- 1.31 .rstrip()
- 1.32 .split()
- 1.33 .splitlines()
- 1.34 .startswith()
- 1.35 .strip()
- 1.36 .swapcase()
- 1.37 .title()
- 1.38 .translate()
- 1.39 .upper()
- 1.40 .zfill()
- 2 Built-in Methods for Lists
- 3 Built-in Methods for Sets
- 4 Built-in Methods for Dictionaries
Built-in Methods for Strings
.capitalize()
<stringobject>.capitalize()
Returns a string where the first character is uppercase. For example:
text = "python for everybody." txt = text.capitalize() print( txt )
Output:
Python for everybody.
.center()
<stringobject>.center(<length>[, <fillchar>])
Returns a string that is aligned to the center using character <fillchar>. The returned string is <length> characters long. If the specified length is less than the <stringobject> string's length, the original <stringobject> will be returned. If you omit the fill character, space will be used as a filler.
For example:
text = "The Core of the Sun" txt = text.center( 25, '-' ) print( txt )
Output:
---The Core of the Sun---
.count()
<stringobject>.count(<substring>[, <start>][, <end>])
Returns the number of times the supplied string <substring> appears in the <stringobject> string. By default .count() searches the <substring> from the beginning of the <stringobject> string to the end of the string.
You can change the search range with the start and end parameters. The start parameter specifies the string position where the search range should start (0 is the start of the string) and the end parameter the string position where the search should end.
For example:
text = "I love AmigaOS. AmigaOS is my favourite operating system." times = text.count( "AmigaOS" ) print( times )
Output:
2
.decode()
<stringobject>.decode([encoding=<encoding>][, errors=<errors>])
Decodes an encoded <stringobject> string and returns the result to the caller. The optional encoding parameter specifies the codec to use and the optional errors parameter the error handling method. If the encoding parameter is omitted, codec UTF-8 will be used.
The full list of available codecs is as follows:
Codec | Aliases | Languages |
---|---|---|
ascii | 646, us-ascii | English |
big5 | big5-tw, csbig5 | Traditional Chinese |
big5hkscs | big5-hkscs, hkscs | Traditional Chinese |
cp037 | IBM037, IBM039 | English |
cp424 | EBCDIC-CP-HE, IBM424 | Hebrew |
cp437 | 437, IBM437 | English |
cp500 | EBCDIC-CP-BE, EBCDIC-CP-CH, IBM500 | Western Europe |
cp720 | Arabic | |
cp737 | Greek | |
cp775 | IBM775 | Baltic languages |
cp850 | 850, IBM850 | Western Europe |
cp852 | 852, IBM852 | Central and Eastern Europe |
cp855 | 855, IBM855 | Bulgarian, Byelorussian, Macedonian, Russian, Serbian |
cp856 | Hebrew | |
cp857 | 857, IBM857 | Turkish |
cp858 | 858, IBM858 | Western Europe |
cp860 | 860, IBM860 | Portuguese |
cp861 | 861, CP-IS, IBM861 | Icelandic |
cp862 | 862, IBM862 | Hebrew |
cp863 | 863, IBM863 | Canadian |
cp864 | IBM864 | Arabic |
cp865 | 865, IBM865 | Danish, Norwegian |
cp866 | 866, IBM866 | Russian |
cp869 | 869, CP-GR, IBM869 | Greek |
cp874 | Thai | |
cp875 | Greek | |
cp932 | 932, ms932, mskanji, ms-kanji | Japanese |
cp949 | 949, ms949, uhc | Korean |
cp950 | 950, ms950 | Traditional Chinese |
cp1006 | Urdu | |
cp1026 | ibm1026 | Turkish |
cp1140 | ibm1140 | Western Europe |
cp1250 | windows-1250 | Central and Eastern Europe |
cp1251 | windows-1251 | Bulgarian, Byelorussian, Macedonian, Russian, Serbian |
cp1252 | windows-1252 | Western Europe |
cp1253 | windows-1253 | Greek |
cp1254 | windows-1254 | Turkish |
cp1255 | windows-1255 | Hebrew |
cp1256 | windows-1256 | Arabic |
cp1257 | windows-1257 | Baltic languages |
cp1258 | windows-1258 | Vietnamese |
euc_jp | eucjp, ujis, u-jis | Japanese |
euc_jis_2004 | jisx0213, eucjis2004 | Japanese |
euc_jisx0213 | eucjisx0213 | Japanese |
euc_kr | euckr, korean, ksc5601, ks_c-5601, ks_c-5601-1987, ksx1001, ks_x-1001 | Korean |
gb2312 | chinese, csiso58gb231280, euc- cn, euccn, eucgb2312-cn, gb2312-1980, gb2312-80, iso- ir-58 | Simplified Chinese |
gbk | 936, cp936, ms936 | Unified Chinese |
gb18030 | gb18030-2000 | Unified Chinese |
hz | hzgb, hz-gb, hz-gb-2312 | Simplified Chinese |
iso2022_jp | csiso2022jp, iso2022jp, iso-2022-jp | Japanese |
iso2022_jp_1 | iso2022jp-1, iso-2022-jp-1 | Japanese |
iso2022_jp_2 | iso2022jp-2, iso-2022-jp-2 | Japanese, Korean, Simplified Chinese, Western Europe, Greek |
iso2022_jp_2004 | iso2022jp-2004, iso-2022-jp-2004 | Japanese |
iso2022_jp_3 | iso2022jp-3, iso-2022-jp-3 | Japanese |
iso2022_jp_ext | iso2022jp-ext, iso-2022-jp-ext | Japanese |
iso2022_kr | csiso2022kr, iso2022kr, iso-2022-kr | Korean |
latin_1 | iso-8859-1, iso8859-1, 8859, cp819, latin, latin1, L1 | West Europe |
iso8859_2 | iso-8859-2, latin2, L2 | Central and Eastern Europe |
iso8859_3 | iso-8859-3, latin3, L3 | Esperanto, Maltese |
iso8859_4 | iso-8859-4, latin4, L4 | Baltic languages |
iso8859_5 | iso-8859-5, cyrillic | Bulgarian, Byelorussian, Macedonian, Russian, Serbian |
iso8859_6 | iso-8859-6, arabic | Arabic |
iso8859_7 | iso-8859-7, greek, greek8 | Greek |
iso8859_8 | iso-8859-8, hebrew | Hebrew |
iso8859_9 | iso-8859-9, latin5, L5 | Turkish |
iso8859_10 | iso-8859-10, latin6, L6 | Nordic languages |
iso8859_11 | iso-8859-11, thai | Thai languages |
iso8859_13 | iso-8859-13, latin7, L7 | Baltic languages |
iso8859_14 | iso-8859-14, latin8, L8 | Celtic languages |
iso8859_15 | iso-8859-15, latin9, L9 | Western Europe |
iso8859_16 | iso-8859-16, latin10, L10 | South-Eastern Europe |
johab | cp1361, ms1361 | Korean |
koi8_r | Russian | |
koi8_u | Ukrainian | |
mac_cyrillic | maccyrillic | Bulgarian, Byelorussian, Macedonian, Russian, Serbian |
mac_greek | macgreek | Greek |
mac_iceland | maciceland | Icelandic |
mac_latin2 | maclatin2, maccentraleurope | Central and Eastern Europe |
mac_roman | macroman | Western Europe |
mac_turkish | macturkish | Turkish |
ptcp154 | csptcp154, pt154, cp154, cyrillic-asian | Kazakh |
shift_jis | csshiftjis, shiftjis, sjis, s_jis | Japanese |
shift_jis_2004 | shiftjis2004, sjis_2004, sjis2004 | Japanese |
shift_jisx0213 | shiftjisx0213, sjisx0213, s_jisx0213 | Japanese |
utf_32 | U32, utf32 | All languages |
utf_32_be | UTF-32BE | All languages |
utf_32_le | UTF-32LE | All languages |
utf_16 | U16, utf16 | All languages |
utf_16_be | UTF-16BE | All languages (BMP only) |
utf_16_le | UTF-16LE | All languages (BMP only) |
utf_7 | U7, unicode-1-1-utf-7 | All languages |
utf_8 | U8, UTF, utf8 | All languages |
utf_8_sig | All languages |
The errors parameter can have one of the following values:
Value | Description |
---|---|
backslashreplace | Uses a backslash instead of the character that could not be decoded. |
ignore | Ignores the characters that cannot be decoded. |
namereplace | Replaces the character with a text explaining the character. |
strict | Raises an error on failure. The default value. |
replace | Replaces the character with a questionmark. |
xmlcharrefreplace | Replaces the character with an XML character. |
- See also
.encode()
<stringobject>.encode([encoding=<encoding>][, errors=<errors>])
This method encodes the string <stringobject> and returns the result to the caller. The optional encoding parameter specifies the encoding codec to be used. See .decode() for available codecs. If the codec is omitted, UTF-8 will be used.
The optional errors parameter allows you to change the handling scheme. See .decode() for the legal values.
- See also
.endswith()
<stringobject>.endswith(<suffix>[, <start>[, <end>]])
The endswith() method returns True if the string <stringobject> ends with the supplied string <suffix>. Otherwise it returns False. By default the method searches the string <stringobject> from start to end. You can change the search range with the start and end parameters. The start parameter specifies the string position where the search range should start (0 is the start of the string) and the end parameter the string position where the search should end.
For example:
text = "This is the house that Jack built." print( text.endswith( "." ) )
Output:
True
.expandtabs()
<stringobject>.expandtabs(<tabsize>)
.find()
<stringobject>.find(<string>, <start>, <end>)
.index()
<stringobject>.index(<string>, <start>, <end>)
.isalnum()
<stringobject>.isalnum()
.isalpha()
<stringobject>.isalpha()
.isdecimal()
<stringobject>.isdecimal()
.isdigit()
<stringobject>.isdigit()
Returns true if the string <stringobject> contains only digits.
.islower()
<stringobject>.islower()
.isnumeric()
<stringobject>.isnumeric()
.isspace()
<stringobject>.isspace()
.istitle()
<stringobject>.istitle()
.isupper()
<stringobject>.isupper()
.join()
<stringobject>.join(sequence)
.len()
<stringobject>.len()
Returns the length of the string <stringobject>.
.ljust()
<stringobject>.ljust(<width>[, <fillchar>])
.lower()
<stringobject>.lower()
.lstrip()
<stringobject>.lstrip()
.maketrans()
<stringobject>.maketrans()
.max()
<stringobject>.max(string)
.min()
<stringobject>.min(<string>)
.replace()
<stringobject>.replace(<old>, <new>[, <max>])
.rfind()
<stringobject>.rfind(<string>, <start>, <end>)
.rindex()
<stringobject>.rindex(<string>, <start>, <end>)
.rjust()
<stringobject>.rjust(<width>[, <fillchar>])
.rstrip()
<stringobject>.rstrip()
.split()
<stringobject>.split(<delimiter>, <splits>)
.splitlines()
<stringobject>.splitlines([<keeplinebreaks>])
Splits the string <stringobject> into a list. The splitting is done at line breaks. The optional parameter <keeplinebreaks> specifies if the line breaks should be included (True), or not (False). Default value is False
.startswith()
<stringobject>.startswith(<string>, <start>, <end>)
.strip()
<stringobject>.strip([<characters>])
Returns a copy of the string <stringobject> where leading and trailing characters are removed. By default the method removes all whitespace characters, but this can be changed with the optional <characters> parameter. The <characters> parameter lists a set of characters which will be removed from the beginning and end of the string.
- Example 1
Remove all leading and trailing whitespaces from string text:
text = " 25th Century " strippedText = text.strip() print( '"'" + strippedText + '"' )
Output:
"25th Century"
- Example 2
Remove all of the following characters from the beginning and end of string text: space, "-", "=", "*", "(", and ")":
text = "--= (*) =-- 25th Century ---= (*) =--" strippedText = text.strip( " -=*()" ) print( '"'" + strippedText + '"' )
Output:
"25th Century"
.swapcase()
<stringobject>.swapcase()
.title()
<stringobject>.title()
.translate()
<stringobject>.translate(<translationtable>[, <deletechars>])
.upper()
<stringobject>.upper()
.zfill()
stringobject.zfill(width)