20.09.2019
Posted by 

— Common string operations Source code: The module contains a number of useful constants and classes, as well as some deprecated legacy functions that are also available as methods on strings. In addition, Python’s built-in string classes support the sequence type methods described in the section, and also the string-specific methods described in the section. To output formatted strings use template strings or the% operator described in the section. Also, see the module for string functions based on regular expressions. String constants The constants defined in this module are: string.

Asciiletters The concatenation of the and constants described below. This value is not locale-dependent.

Python

Asciilowercase The lowercase letters 'abcdefghijklmnopqrstuvwxyz'. This value is not locale-dependent and will not change. Asciiuppercase The uppercase letters 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. This value is not locale-dependent and will not change. Digits The string '. Hexdigits The string 'abcdefABCDEF'.

Python Strings - Learn Python in simple and easy steps starting from basic to advanced concepts with examples including Python Syntax. Format - Performs String. String – Working with text. String templates were added in Python 2.4 as part of PEP 292 and are intended as an alternative to the built-in interpolation syntax.

Letters The concatenation of the strings and described below. The specific value is locale-dependent, and will be updated when is called.

Lowercase A string containing all the characters that are considered lowercase letters. On most systems this is the string 'abcdefghijklmnopqrstuvwxyz'. The specific value is locale-dependent, and will be updated when 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 of, and.

Uppercase A string containing all the characters that are considered uppercase letters. On most systems this is the string 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. The specific value is locale-dependent, and will be updated when 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.

New in version 2.6. The built-in str and unicode classes provide the ability to do complex variable substitutions and value formatting via the method described in. The class in the module allows you to create and customize your own string formatting behaviors using the same implementation as the built-in method.

Class string. Formatter The class has the following public methods: format ( formatstring,.args,.kwargs ) The primary API method.

It takes a format string and an arbitrary set of positional and keyword arguments. It is just a wrapper that calls. Vformat ( formatstring, args, kwargs ) This function does the actual work of formatting. It is exposed as a separate function for cases where you want to pass in a predefined dictionary of arguments, rather than unpacking and repacking the dictionary as individual arguments using the.args and.kwargs syntax. Does the work of breaking up the format string into character data and replacement fields. It calls the various methods described below.

In addition, the defines a number of methods that are intended to be replaced by subclasses: parse ( formatstring ) Loop over the formatstring and return an iterable of tuples ( literaltext, fieldname, formatspec, conversion). This is used by to break the string into either literal text, or replacement fields. The values in the tuple conceptually represent a span of literal text followed by a single replacement field. If there is no literal text (which can happen if two replacement fields occur consecutively), then literaltext will be a zero-length string. If there is no replacement field, then the values of fieldname, formatspec and conversion will be None.

Getfield ( fieldname, args, kwargs ) Given fieldname as returned by (see above), convert it to an object to be formatted. Returns a tuple (obj, usedkey). The default version takes strings of the form defined in, such as “0name” or “label.title”.

Args and kwargs are as passed in to. The return value usedkey has the same meaning as the key parameter to. Getvalue ( key, args, kwargs ) Retrieve a given field value.

The key argument will be either an integer or a string. If it is an integer, it represents the index of the positional argument in args; if it is a string, then it represents a named argument in kwargs. The args parameter is set to the list of positional arguments to, and the kwargs parameter is set to the dictionary of keyword arguments. For compound field names, these functions are only called for the first component of the field name; Subsequent components are handled through normal attribute and indexing operations.

So for example, the field expression ‘0.name’ would cause to be called with a key argument of 0. The name attribute will be looked up after returns by calling the built-in function. If the index or keyword refers to an item that does not exist, then an or should be raised. Checkunusedargs ( usedargs, args, kwargs ) Implement checking for unused arguments if desired.

The arguments to this function is the set of all argument keys that were actually referred to in the format string (integers for positional arguments, and strings for named arguments), and a reference to the args and kwargs that was passed to vformat. The set of unused args can be calculated from these parameters. Is assumed to raise an exception if the check fails. Formatfield ( value, formatspec ) simply calls the global built-in. The method is provided so that subclasses can override it. Convertfield ( value, conversion ) Converts the value (returned by ) given a conversion type (as in the tuple returned by the method). The default version understands ‘s’ (str), ‘r’ (repr) and ‘a’ (ascii) conversion types.

Python Template String

Option Meaning ' Forces the field to be right-aligned within the available space (this is the default for numbers). '=' Forces the padding to be placed after the sign (if any) but before the digits. This is used for printing fields in the form ‘+000000120’. This alignment option is only valid for numeric types. It becomes the default when ‘0’ immediately precedes the field width.

'^' Forces the field to be centered within the available space. Note that unless a minimum field width is defined, the field width will always be the same size as the data to fill it, so that the alignment option has no meaning in this case.

The sign option is only valid for number types, and can be one of the following. Option Meaning '+' indicates that a sign should be used for both positive as well as negative numbers. '-' indicates that a sign should be used only for negative numbers (this is the default behavior). Space indicates that a leading space should be used on positive numbers, and a minus sign on negative numbers. The '#' option is only valid for integers, and only for binary, octal, or hexadecimal output. If present, it specifies that the output will be prefixed by '0b', '0o', or '0x', respectively.

The ',' option signals the use of a comma for a thousands separator. For a locale aware separator, use the 'n' integer presentation type instead. Changed in version 2.7: Added the ',' option (see also ).

Width is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content. When no explicit alignment is given, preceding the width field by a zero ( '0') character enables sign-aware zero-padding for numeric types.

This is equivalent to a fill character of '0' with an alignment type of '='. The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with 'f' and 'F', or before and after the decimal point for a floating point value formatted with 'g' or 'G'.

For non-number types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The precision is not allowed for integer values. Finally, the type determines how the data should be presented. The available string presentation types are.

Type Meaning 'b' Binary format. Outputs the number in base 2. 'c' Character. Converts the integer to the corresponding unicode character before printing. 'd' Decimal Integer. Outputs the number in base 10. 'o' Octal format.

Outputs the number in base 8. 'x' Hex format.

Outputs the number in base 16, using lower- case letters for the digits above 9. 'X' Hex format. Outputs the number in base 16, using upper- case letters for the digits above 9. This is the same as 'd', except that it uses the current locale setting to insert the appropriate number separator characters.

None The same as 'd'. In addition to the above presentation types, integers can be formatted with the floating point presentation types listed below (except 'n' and None). When doing so, is used to convert the integer to a floating point number before formatting. The available presentation types for floating point and decimal values are. Type Meaning 'e' Exponent notation. Prints the number in scientific notation using the letter ‘e’ to indicate the exponent. The default precision is 6.

'E' Exponent notation. Same as 'e' except it uses an upper case ‘E’ as the separator character. 'f' Fixed point. Displays the number as a fixed-point number. The default precision is 6.

'F' Fixed point. 'g' General format.

For a given precision p = 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude. The precise rules are as follows: suppose that the result formatted with presentation type 'e' and precision p-1 would have exponent exp.

from string import Template s = Template ( '$who likes $what' ) s. Substitute ( who = 'tim', what = 'kung pao' ) 'tim likes kung pao' d = dict ( who = 'tim' ) Template ( 'Give $who $100' ). Substitute ( d ) Traceback (most recent call last). ValueError: Invalid placeholder in string: line 1, col 11 Template ( '$who likes $what' ). Substitute ( d ) Traceback (most recent call last). KeyError: 'what' Template ( '$who likes $what' ). Safesubstitute ( d ) 'tim likes $what' Advanced usage: you can derive subclasses of to customize the placeholder syntax, delimiter character, or the entire regular expression used to parse template strings.

To do this, you can override these class attributes:. delimiter – This is the literal string describing a placeholder introducing delimiter.

Python Template Substitution

The default value is $. Note that this should not be a regular expression, as the implementation will call on this string as needed. idpattern – This is the regular expression describing the pattern for non-braced placeholders (the braces will be added automatically as appropriate). The default value is the regular expression a-za-z0-9. Alternatively, you can provide the entire regular expression pattern by overriding the class attribute pattern. If you do this, the value must be a regular expression object with four named capturing groups. The capturing groups correspond to the rules given above, along with the invalid placeholder rule:.

escaped – This group matches the escape sequence, e.g. $$, in the default pattern. named – This group matches the unbraced placeholder name; it should not include the delimiter in capturing group.

braced – This group matches the brace enclosed placeholder name; it should not include either the delimiter or braces in the capturing group. invalid – This group matches any other delimiter pattern (usually a single delimiter), and it should appear last in the regular expression. String functions The following functions are available to operate on string and Unicode objects.

They are not available as string methods. Capwords ( s , sep ) Split the argument into words using, capitalize each word using, and join the capitalized words using. If the optional second argument sep is absent or None, runs of whitespace characters are replaced by a single space and leading and trailing whitespace are removed, otherwise sep is used to split and join the words. Maketrans ( from, to ) Return a translation table suitable for passing to, that will map each character in from into the character at the same position in to; from and to must have the same length. Deprecated since version 2.0: Use the built-in function.

Convert string s to an integer in the given base. The string must consist of one or more digits, optionally preceded by a sign ( + or -). The base defaults to 10.

Python Template String

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. If base is 16, a leading 0x or 0X is always accepted, though not required. This behaves identically to the built-in function when passed a string. (Also note: for a more flexible interpretation of numeric literals, use the built-in function.) string. Atol ( s , base ). Deprecated since version 2.0: Use the built-in function. Convert string s to a long integer in the given base.

The string must consist of one or more digits, optionally preceded by a sign ( + or -). The base argument has the same meaning as for. A trailing l or L is not allowed, except if the base is 0. Note that when invoked without base or with base set to 10, this behaves identical to the built-in function when passed a string.

Capitalize ( word ) Return a copy of word with only its first character capitalized. Expandtabs ( s , tabsize ) Expand tabs in a string replacing 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 in s where the substring sub is found such that sub is wholly contained in sstart:end. Return -1 on failure. Defaults for start and end and interpretation of negative values is the same as for slices. Rfind ( s, sub , start , end ) Like but find the highest index. Index ( s, sub , start , end ) Like but raise when the substring is not found. Rindex ( s, sub , start , end ) Like but raise when the substring is not found.

Count ( s, sub , start , end ) Return the number of (non-overlapping) occurrences of substring sub in string sstart:end. Defaults for start and end and interpretation of negative values are the same as for slices.

Lower ( s ) Return a copy of s, but with upper case letters converted to lower case. Split ( s , sep , maxsplit ) Return a list of the words of the string s. If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). If the second argument sep is present and not None, 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.

If maxsplit is given, at most maxsplit 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 most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made). The behavior of split on an empty string depends on the value of sep. If sep is not specified, or specified as None, the result will be an empty list.

Python Template

If sep is specified as any string, the result will be a list containing one element which is an empty string. Rsplit ( s , sep , maxsplit ) Return a list of the words of the string s, scanning s from the end. To all intents and purposes, the resulting list of words is the same as returned by, except when the optional third argument maxsplit is explicitly specified and nonzero. If maxsplit is given, at most maxsplit number of splits – the rightmost ones – occur, and the remainder of the string is returned as the first element of the list (thus, the list will have at most maxsplit+1 elements). New in version 2.4. Splitfields ( s , sep , maxsplit ) This function behaves identically to. (In the past, was only used with one argument, while was only used with two arguments.) string.

Ametek pvhcl-s1. PVHCL-S1 and PSCL-S1 Ametek, Pentek, US Filter, Plymouth, Culligan, American Plumber and Kleen Plus Sediment Water Filters Kleen-Plus water filters and Ametek KleenPlus water filter for whole house systems at Waterfilters.net. Replacement Ametek Kleen-Plus water filters for whole house.

Join ( words , sep ) Concatenate a list or tuple of words with intervening occurrences of sep. The default value for sep is a single space character. It is always true that string.join(string.split(s, sep), sep) equals s. Joinfields ( words , sep ) This function behaves identically to. (In the past, was only used with one argument, while was only used with two arguments.) Note that there is no method on string objects; use the method instead. Lstrip ( s , chars ) Return a copy of the string with leading characters removed.

If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the beginning of the string this method is called on.

Python Template Library

Changed in version 2.2.3: The chars parameter was added. The chars parameter cannot be passed in earlier 2.2 versions. Swapcase ( s ) Return a copy of s, but with lower case letters converted to upper case and vice versa. Translate ( s, table , deletechars ) Delete all characters from s that are in deletechars (if present), and then translate the characters using table, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. If table is None, then only the character deletion step is performed.

Upper ( s ) Return a copy of s, but with lower case letters converted to upper case. Ljust ( s, width , fillchar ) string. Rjust ( s, width , fillchar ) string.

Center ( s, width , fillchar ) These functions respectively left-justify, right-justify and center a string in a field of given width. They return a string that is at least width characters wide, created by padding the string s with the character fillchar (default is a space) until the given width on the right, left or both sides. The string is never truncated. Zfill ( s, width ) Pad a numeric string s on the left with zero digits until the given width is reached. Strings starting with a sign are handled correctly. Replace ( s, old, new , maxreplace ) Return a copy of string s with all occurrences of substring old replaced by new.

If the optional argument maxreplace is given, the first maxreplace occurrences are replaced.