Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:4. String ServicesUp:4. String ServicesNext:4.2 re

4.1string -- Common string operations

This module defines some constants useful for checking characterclasses and some useful string functions. See the modulere for string functions based on regularexpressions.

The constants defined in this module are:

ascii_letters
The concatenation of theascii_lowercase andascii_uppercase constants described below. This value is not locale-dependent.

ascii_lowercase
The lowercase letters'abcdefghijklmnopqrstuvwxyz'. This value is not locale-dependent and will not change.

ascii_uppercase
The uppercase letters'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. This value is not locale-dependent and will not change.

digits
The string'0123456789'.

hexdigits
The string'0123456789abcdefABCDEF'.

letters
The concatenation of the stringslowercase anduppercase described below. The specific value is locale-dependent, and will be updated whenlocale.setlocale() is called.

lowercase
A string containing all the characters that are considered lowercase letters. On most systems this is the string'abcdefghijklmnopqrstuvwxyz'. Do not change its definition -- the effect on the routinesupper() andswapcase() is undefined. The specific value is locale-dependent, and will be updated whenlocale.setlocale() is called.

octdigits
The string'01234567'.

punctuation
String of ASCII characters which are considered punctuation characters in the "C" locale.

printable
String of characters which are considered printable. This is a combination ofdigits,letters,punctuation, andwhitespace.

uppercase
A string containing all the characters that are considered uppercase letters. On most systems this is the string'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. Do not change its definition -- the effect on the routineslower() andswapcase() is undefined. The specific value is locale-dependent, and will be updated whenlocale.setlocale() is called.

whitespace
A string containing all characters that are considered whitespace. On most systems this includes the characters space, tab, linefeed, return, formfeed, and vertical tab. Do not change its definition -- the effect on the routinesstrip() andsplit() is undefined.

Many of the functions provided by this module are also defined asmethods of string and Unicode objects; see ``String Methods'' (section2.2.6) for more information on those.The functions defined in this module are:

atof(s)
Deprecated since release 2.0.Use thefloat() built-in function.

Convert a string to a floating point number. The string must have the standard syntax for a floating point literal in Python, optionally preceded by a sign ("+" or "-"). Note that this behaves identical to the built-in functionfloat() when passed a string.

Note:When passing in a string, values for NaN  and Infinity may be returned, depending on the underlying C library. The specific set of strings accepted which cause these values to be returned depends entirely on the C library and is known to vary.

atoi(s[, base])
Deprecated since release 2.0.Use theint() built-in function.

Convert strings to an integer in the givenbase. The string must consist of one or more digits, optionally preceded by a sign ("+" or "-"). Thebase defaults to 10. If it is 0, a default base is chosen depending on the leading characters of the string (after stripping the sign): "0x" or "0X" means 16, "0" means 8, anything else means 10. Ifbase is 16, a leading "0x" or "0X" is always accepted, though not required. This behaves identically to the built-in functionint() when passed a string. (Also note: for a more flexible interpretation of numeric literals, use the built-in functioneval() .)

atol(s[, base])
Deprecated since release 2.0.Use thelong() built-in function.

Convert strings to a long integer in the givenbase. The string must consist of one or more digits, optionally preceded by a sign ("+" or "-"). Thebase argument has the same meaning as foratoi(). A trailing "l" or "L" is not allowed, except if the base is 0. Note that when invoked withoutbase or withbase set to 10, this behaves identical to the built-in functionlong() when passed a string.

capitalize(word)
Return a copy ofword with only its first character capitalized.

capwords(s)
Split the argument into words usingsplit(), capitalize each word usingcapitalize(), and join the capitalized words usingjoin(). Note that this replaces runs of whitespace characters by a single space, and removes leading and trailing whitespace.

expandtabs(s[, tabsize])
Expand tabs in a string, i.e. replace them by one or more spaces, depending on the current column and the given tab size. The column number is reset to zero after each newline occurring in the string. This doesn't understand other non-printing characters or escape sequences. The tab size defaults to 8.

find(s, sub[, start[,end]])
Return the lowest index ins where the substringsub is found such thatsub is wholly contained ins[start:end]. Return-1 on failure. Defaults forstart andend and interpretation of negative values is the same as for slices.

rfind(s, sub[, start[, end]])
Likefind() but find the highest index.

index(s, sub[, start[, end]])
Likefind() but raiseValueError when the substring is not found.

rindex(s, sub[, start[, end]])
Likerfind() but raiseValueError when the substring is not found.

count(s, sub[, start[, end]])
Return the number of (non-overlapping) occurrences of substringsub in strings[start:end]. Defaults forstart andend and interpretation of negative values are the same as for slices.

lower(s)
Return a copy ofs, but with upper case letters converted to lower case.

maketrans(from, to)
Return a translation table suitable for passing totranslate() orregex.compile(), that will map each character infrom into the character at the same position into;from andto must have the same length.

Warning:Don't use strings derived fromlowercase anduppercase as arguments; in some locales, these don't have the same length. For case conversions, always uselower() andupper().

split(s[, sep[, maxsplit]])
Return a list of the words of the strings. If the optional second argumentsep is absent orNone, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). If the second argumentsep is present and notNone, it specifies a string to be used as the word separator. The returned list will then have one more item than the number of non-overlapping occurrences of the separator in the string. The optional third argumentmaxsplit defaults to 0. If it is nonzero, at mostmaxsplit number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at mostmaxsplit+1 elements).

splitfields(s[, sep[, maxsplit]])
This function behaves identically tosplit(). (In the past,split() was only used with one argument, whilesplitfields() was only used with two arguments.)

join(words[, sep])
Concatenate a list or tuple of words with intervening occurrences ofsep. The default value forsep is a single space character. It is always true that "string.join(string.split(s,sep),sep)" equalss.

joinfields(words[, sep])
This function behaves identically tojoin(). (In the past,join() was only used with one argument, whilejoinfields() was only used with two arguments.) Note that there is nojoinfields() method on string objects; use thejoin() method instead.

lstrip(s[, chars])
Return a copy of the string with leading characters removed. Ifchars is omitted orNone, whitespace characters areremoved. If given and notNone,chars must be a string;the characters in the string will be stripped from the beginning ofthe string this method is called on.Changed in version 2.2.3:Thechars parameter was added. Thechars parameter cannot be passed in earlier 2.2 versions.

rstrip(s[, chars])
Return a copy of the string with trailing characters removed. Ifchars is omitted orNone, whitespace characters areremoved. If given and notNone,chars must be a string;the characters in the string will be stripped from the end of thestring this method is called on.Changed in version 2.2.3:Thechars parameter was added. Thechars parameter cannot be passed in earlier 2.2 versions.

strip(s[, chars])
Return a copy of the string with leading and trailing charactersremoved. Ifchars is omitted orNone, whitespacecharacters are removed. If given and notNone,charsmust be a string; the characters in the string will be stripped fromthe both ends of the string this method is called on.Changed in version 2.2.3:Thechars parameter was added. Thechars parameter cannot be passed in earlier 2.2 versions.

swapcase(s)
Return a copy ofs, but with lower case letters converted to upper case and vice versa.

translate(s, table[, deletechars])
Delete all characters froms that are indeletechars (if present), and then translate the characters usingtable, which must be a 256-character string giving the translation for each character value, indexed by its ordinal.

upper(s)
Return a copy ofs, but with lower case letters converted to upper case.

ljust(s, width)
rjust(s, width)
center(s, width)
These functions respectively left-justify, right-justify and center a string in a field of given width. They return a string that is at leastwidth characters wide, created by padding the strings with spaces until the given width on the right, left or both sides. The string is never truncated.

zfill(s, width)
Pad a numeric string on the left with zero digits until the given width is reached. Strings starting with a sign are handled correctly.

replace(str, old, new[, maxsplit])
Return a copy of stringstr with all occurrences of substringold replaced bynew. If the optional argumentmaxsplit is given, the firstmaxsplit occurrences are replaced.


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:4. String ServicesUp:4. String ServicesNext:4.2 re
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp