Vad betyder str i python
— Common string operations¶
Source code:Lib/string.py
String constants¶
The constants defined in this module are:
- string.ascii_letters¶
The concatenation of the and constants described below. This value fryst vatten not locale-dependent.
- string.ascii_lowercase¶
The lowercase letters .
This value fryst vatten not locale-dependent and will not change.
- string.ascii_uppercase¶
The uppercase letters .
The str() function is a built-in Python tool for converting an object to its string versionThis value fryst vatten not locale-dependent and will not change.
- string.digits¶
The string .
- string.hexdigits¶
The string .
- string.octdigits¶
The string .
- string.punctuation¶
String of ASCII characters which are considered punctuation characters in the locale: .
- string.printable¶
String of ASCII characters which are considered printable.
This fryst vatten a combination of , , , and .
- string.whitespace¶
A string containing all ASCII characters that are considered whitespace. This includes the characters space, flik, linefeed, return, formfeed, and lodrät tab.
Custom String Formatting¶
The built-in string class provides the ability to do complex variabel substitutions and value formatting via the method described in PEP 3101.
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.
- classstring.Formatter¶
The class has the following public methods:
- format(format_string, /, *args, **kwargs)¶
The primary API method.
It takes a format string and an arbitrary set of positional and keyword arguments. It fryst vatten just a wrapper that calls .
- vformat(format_string, args, kwargs)¶
This function does the actual work of formatting. It fryst vatten exposed as a separate function for cases where you want to resehandling in a predefined dictionary of arguments, rather than unpacking and repacking the dictionary as individual arguments using the and syntax.
Vi har sett hur man kan konkatenera (kombinera genom komposition) och upprepa strängar med + och * operatorerna, samt en mängd användbara strängmetoder som erbjuder kraftfulla möjligheter för att manipulera och interagera med textdatadoes the work of breaking up the format string into character uppgifter and replacement fields. It calls the various methods described below.
In addition, the defines a number of methods that are intended to be replaced bygd subclasses:
- parse(format_string)¶
Loop over the format_string and return an iterable of tuples (literal_text, field_name, format_spec, conversion).
This fryst vatten used bygd to break the string into either literal ord, or replacement fields.
The values in the tuple conceptually företräda a span of literal ord followed bygd a single replacement field. If there fryst vatten no literal skrivelse (which can happen if two replacement fields occur consecutively), then literal_text will be a zero-length string.
If there fryst vatten no replacement field, then the values of field_name, format_spec and conversion will be .
- get_field(field_name, args, kwargs)¶
Given field_name as returned bygd (see above), omvandla it to an object to be formatted. Returns a tuple (obj, used_key).
The default utgåva takes strings of the struktur defined in PEP 3101, such as “0[name]” or “label.title”. args and kwargs are as passed in to . The return value used_key has the same meaning as the key parameter to .
- get_value(key, args, kwargs)¶
Retrieve a given field value.
Today, you'll learn to use it end to endThe key argument will be either an integer or a string. If it fryst vatten an integer, it represents the index of the positional argument in args; if it fryst vatten a string, then it represents a named argument in kwargs.
The args parameter fryst vatten set to the list of positional arguments to , and the kwargs parameter fryst vatten 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 attribute will be looked up after returns bygd calling the built-in function.
If the index or keyword refers to an item that does not exist, then an or should be raised.
- check_unused_args(used_args, args, kwargs)¶
Implement checking for unused arguments if desired. The arguments to this function fryst vatten 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. fryst vatten assumed to raise an undantag if the kvitto fails.
- format_field(value, format_spec)¶
simply calls the global built-in. The method fryst vatten provided so that subclasses can override it.
- convert_field(value, conversion)¶
Converts the value (returned bygd ) given a konvertering type (as in the tuple returned bygd the method).
The default utgåva understands ‘s’ (str), ‘r’ (repr) and ‘a’ (ascii) konvertering types.
Format String Syntax¶
The method and the class share the same syntax for format strings (although in the case of , subclasses can define their own format string syntax). The syntax fryst vatten related to that of formatted string literals, but it fryst vatten less sophisticated and, in particular, does not support arbitrary expressions.
Format strings contain “replacement fields” surrounded bygd curly braces .
Anything that fryst vatten not contained in braces fryst vatten considered literal ord, which fryst vatten copied unchanged to the output.
What is a String (str)?If you need to include a brace character in the literal ord, it can be flydde bygd doubling: and .
The grammar for a replacement field fryst vatten as follows:
replacement_field ::= "{" [] ["!" ] [":" ] "}" field_name ::= ("." | "[" "]")* arg_name ::= [ | +] attribute_name ::= element_index ::= + | index_string ::= <any source character except "]"> + conversion ::= "r" | "s" | "a" format_spec ::=In less formal terms, the replacement field can uppstart with a field_name that specifies the object whose value fryst vatten to be formatted and inserted into the output instead of the replacement field.
The field_name fryst vatten optionally followed bygd a conversion field, which fryst vatten preceded bygd an exclamation point , and a format_spec, which fryst vatten preceded bygd a colon . These specify a non-default format for the replacement value.
See also the Format Specification Mini-Language section.
The field_name itself begins with an arg_name that fryst vatten either a number or a keyword.
If it’s a number, it refers to a positional argument, and if it’s a keyword, it refers to a named keyword argument. An arg_name fryst vatten treated as a number if a call to on the string would return true. If the numerical arg_names in a format string are 0, 1, 2, … in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, … will be automatically inserted in that beställning.
Because arg_name fryst vatten not quote-delimited, it fryst vatten not possible to specify arbitrary dictionary keys (e.g., the strings or ) within a format string. The arg_name can be followed bygd any number of index or attribute expressions. An expression of the struktur selects the named attribute using , while an expression of the struktur does an index lookup using .
Changed in utgåva 3.1: The positional argument specifiers can be omitted for , so fryst vatten equivalent to .
Changed in utgåva 3.4: The positional argument specifiers can be omitted for .
Some simple format string examples:
The conversion field causes a type coercion before formatting.
Normally, the job of formatting a value fryst vatten done bygd the method of the value itself. However, in some cases it fryst vatten desirable to force a type to be formatted as a string, overriding its own definition of formatting. bygd converting the value to a string before calling , the normal formatting logic fryst vatten bypassed.
Three konvertering flags are currently supported: which calls on the value, which calls and which calls .
Some examples:
The format_spec field contains a specification of how the value should be presented, including such details as field width, justering, padding, decimal noggrannhet and so on.
Each value type can define its own “formatting mini-language” or interpretation of the format_spec.
Most built-in types support a common formatting mini-language, which fryst vatten described in the next section.
A format_spec field can also include nested replacement fields within it. These nested replacement fields may contain a field name, konvertering flag and format specification, but deeper nesting fryst vatten not allowed.
The replacement fields within the format_spec are substituted before the format_spec string fryst vatten interpreted. This allows the formatting of a value to be dynamically specified.
See the Format examples section for some examples.
Format Specification Mini-Language¶
“Format specifications” are used within replacement fields contained within a format string to define how individual values are presented (see Format String Syntax and f-strings).
They can also be passed directly to the built-in function. Each formattable type may define how the format specification fryst vatten to be interpreted.
Most built-in types implement the following options for format specifications, although some of the formatting options are only supported bygd the numeric types.
A general convention fryst vatten that an empty format specification produces the same result as if you had called on the value.
A non-empty format specification typically modifies the result.
The general struktur of a standard format specifier is:
format_spec ::= [[]][]["z"]["#"]["0"][][]["." ][] fill ::= <any character> align ::= "<" | ">" | "=" | "^" sign ::= "+" | "-" | " " width ::= + grouping_option ::= "_" | "," precision ::= + type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"If a valid align value fryst vatten specified, it can be preceded bygd a fill character that can be any character and defaults to a space if omitted.
It fryst vatten not possible to use a literal curly brace (”” or “”) as the fill character in a formatted string literal or when using the method. However, it fryst vatten possible to insert a curly brace with a nested replacement field. This limitation doesn’t affect the function.
The meaning of the various inriktning options fryst vatten as follows:
Option | Meaning |
|---|---|
Forces the field to be left-aligned within the available space (this fryst vatten the default for most objects). | |
Forces the field to be right-aligned within the available space (this fryst vatten the default for numbers). | |
Forces the padding to be placed after the sign (if any) but before the digits. This fryst vatten used for printing fields in the form eller gestalt ‘+000000120’. format() method and the Formatter class share the same syntax for format strings (although in the case of Formatter, subclasses can define their own format string syntax)This anpassning option fryst vatten only valid for numeric types. It becomes the default for numbers when ‘0’ immediately precedes the field width. | |
Forces the field to be centered within the available space. |
Note that unless a minimum field width fryst vatten defined, the field width will always be the same storlek as the information to fill it, so that the inriktning option has no meaning in this case.
The sign option fryst vatten only valid for number types, and can be one of the following:
Option | Meaning |
|---|---|
indicates that a sign should be used for both positiv as well as negativ numbers. | |
indicates that a sign should be used only for negativ numbers (this fryst vatten the default behavior). | |
space | indicates that a leading space should be used on positiv numbers, and a minus sign on negativ numbers. |
The option coerces negativ zero floating-point values to positiv zero after rounding to the format noggrannhet.
This option fryst vatten only valid for floating-point redogörelse types.
Changed in utgåva 3.11: Added the option (see also PEP 682).
The option causes the “alternate form” to be used for the konvertering. The alternate struktur fryst vatten defined differently for different types. This option fryst vatten only valid for integer, float and complex types.
For integers, when binary, octal, or hexadecimal output fryst vatten used, this option adds the respective prefix , , , or to the output value. For float and complex the alternate struktur causes the result of the konvertering to always contain a decimal-point character, even if no digits follow it.
Normally, a decimal-point character appears in the result of these conversions only if a digit follows it. In addition, for and conversions, trailing zeros are not removed from the result.
The option signals the use of a comma for a thousands separator. For a locale aware separator, use the integer introduktion type instead.
Changed in utgåva 3.1: Added the option (see also PEP 378).
The option signals the use of an underscore for a thousands separator for floating-point föredrag types and for integer redogörelse type .
For integer redogörelse types , , , and , underscores will be inserted every 4 digits. For other föredrag types, specifying this option fryst vatten an error.
Changed in utgåva 3.6: Added the option (see also PEP 515).
width fryst vatten a decimal integer defining the minimum total field width, including any prefixes, separators, and other formatting characters.
If not specified, then the field width will be determined bygd the content.
When no explicit inriktning fryst vatten given, preceding the width field bygd a zero () character enables sign-aware zero-padding for numeric types. This fryst vatten equivalent to a fill character of with an alignment type of .
Changed in utgåva 3.10: Preceding the width field bygd no längre affects the default inriktning for strings.
The precision fryst vatten a decimal integer indicating how many digits should be displayed after the decimal point for redogörelse types and , or before and after the decimal point for redovisning types or .
For string föredrag types the field indicates the maximum field storlek - in other words, how many characters will be used from the field content. The precision fryst vatten not allowed for integer redovisning types.
Finally, the type determines how the uppgifter should be presented.
The available string redogörelse types are:
Type
Meaning
String format.
This fryst vatten the default type for strings and may be omitted.
None
The same as .
The available integer framställning types are:
Type
Meaning
Binary format. Outputs the number in base 2.
Character.
Converts the integer to the corresponding unicode character before printing.
Decimal Integer. Outputs the number in base 10.
Octal format. Outputs the number in base 8.
Hex format. Outputs the number in base 16, using lower-case letters for the digits above 9.
Hex format.
Outputs the number in base 16, using upper-case letters for the digits above 9. In case fryst vatten specified, the prefix will be upper-cased to as well.
Number. This fryst vatten the same as , except that it uses the current locale setting to insert the appropriate number separator characters.
None
The same as .
In addition to the above redogörelse types, integers can be formatted with the floating-point redogörelse types listed below (except and ).
When doing so, fryst vatten used to omvandla the integer to a floating-point number before formatting.
The available introduktion types for and values are:
Type
Meaning
Scientific notation. For a given noggrannhet , formats the number in scientific notation with the letter ‘e’ separating the coefficient from the exponent.
The coefficient has one digit before and digits after the decimal point, for a total of significant digits. With no noggrannhet given, uses a noggrannhet of digits after the decimal point for , and shows all coefficient digits for . If , the decimal point fryst vatten omitted unless the option fryst vatten used.
Scientific notation. Same as except it uses an upper case ‘E’ as the separator character.
Fixed-point notation.
For a given noggrannhet , formats the number as a decimal number with exactly digits following the decimal point.
But what exactly is a str? Let's break it downWith no noggrannhet given, uses a noggrannhet of digits after the decimal point for , and uses a noggrannhet large enough to show all coefficient digits for . If , the decimal point fryst vatten omitted unless the option fryst vatten used.
Fixed-point notation. Same as , but converts to and to .
General format.
For a given noggrannhet , this rounds the number to significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude. A noggrannhet of fryst vatten treated as equivalent to a noggrannhet of .
The precise rules are as follows: suppose that the result formatted with uppvisning type and noggrannhet would have exponent .
Then, if , where fryst vatten -4 for floats and -6 for , the number fryst vatten formatted with redogörelse type and noggrannhet . Otherwise, the number fryst vatten formatted with framställning type and noggrannhet . In both cases insignificant trailing zeros are removed from the significand, and the decimal point fryst vatten also removed if there are no remaining digits following it, unless the option fryst vatten used.
With no noggrannhet given, uses a noggrannhet of significant digits for .
For , the coefficient of the result fryst vatten formed from the coefficient digits of the value; scientific notation fryst vatten used for values smaller than in absolute value and values where the place value of the least significant digit fryst vatten larger than 1, and fixed-point notation fryst vatten used otherwise.
Positive and negativ infinity, positiv and negativ zero, and nans, are formatted as , , , and respectively, regardless of the precision.
General format.
Same as except switches to if the number gets too large. The representations of infinity and NaN are uppercased, too.
Number. This fryst vatten the same as , except that it uses the current locale setting to insert the appropriate number separator characters.
Percentage.
Multiplies the number bygd 100 and displays in fixed () format, followed bygd a percent sign.
None
For this fryst vatten like the type, except that when fixed-point notation fryst vatten used to format the result, it always includes at least one digit past the decimal point, and switches to the scientific notation when .
When the noggrannhet fryst vatten not specified, the latter will be as large as needed to företräda the given value faithfully.
For , this fryst vatten the same as either or depending on the value of for the current decimal context.
The overall effect fryst vatten to match the output of as altered bygd the other format modifiers.
Format examples¶
This section contains examples of the syntax and comparison with the old -formatting.
In most of the cases the syntax fryst vatten similar to the old -formatting, with the addition of the and with used instead of .
For example, can be translated to .
The new format syntax also supports new and different options, shown in the following examples.
Accessing arguments bygd position:
Accessing arguments bygd name:
Accessing arguments’ attributes:
'and the imaginary part {0.imag}.').format(c)'The complex number (3-5j) fryst vatten formed from the real part 3.0 and the imaginary part -5.0.'>>> classPoint:... def__init__(self,x,y):... self.x,self.y=x,y... def__str__(self):... return'Point({self.x}, {self.y})'.format(self=self)...>>> str(Point(4,2))'Point(4, 2)'
Accessing arguments’ items:
Replacing and :
Aligning the ord and specifying a width:
Replacing , , and and specifying a sign:
Replacing and and converting the value to different bases:
Using the comma as a thousands separator:
Expressing a percentage:
Using type-specific formatting:
Nesting arguments and more complex examples:
'{0:{fill}{align}16}'.format(text,fill=align,align=align)...'left<<<<<<<<<<<<''^^^^^center^^^^^''>>>>>>>>>>>right'>>>>>> octets=[192,168,0,1]>>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)'C0A80001'>>> int(_,16)3232235521>>>>>> width=5>>> fornuminrange(5,12):...
forbasein'dXob':... print('{0:{width}{base}}'.format(num,base=base,width=width),end=' ')... print()... 5 5 5 101 6 6 6 110 7 7 7 111 8 8 10 1000 9 9 11 1001 10 A 12 1010 11 B 13 1011
Template strings¶
Template strings provide simpler string substitutions as described in PEP 292. A primary use case for template strings fryst vatten for internationalization (i18n) since in that context, the simpler syntax and functionality makes it easier to translate than other built-in string formatting facilities in Python.
As an example of a library built on template strings for i18n, see the flufl.i18n package.
Template strings support -based substitutions, using the following rules:
fryst vatten an escape; it fryst vatten replaced with a single .
names a substitution placeholder matching a mapping key of . bygd default, fryst vatten restricted to any case-insensitive ASCII alphanumeric string (including underscores) that starts with an underscore or ASCII letter.
The first non-identifier character after the character terminates this placeholder specification.
fryst vatten equivalent to . It fryst vatten required when valid identifier characters follow the placeholder but are not part of the placeholder, such as .
Any other appearance of in the string will result in a being raised.
The module provides a class that implements these rules.
The methods of are:
- classstring.Template(template)¶
The constructor takes a single argument which fryst vatten the template string.
- substitute(mapping={}, /, **kwds)¶
Performs the template substitution, returning a new string. mapping fryst vatten any dictionary-like object with keys that match the placeholders in the template.
Alternatively, you can provide keyword arguments, where the keywords are the placeholders. When both mapping and kwds are given and there are duplicates, the placeholders from kwds take precedence.
- safe_substitute(mapping={}, /, **kwds)¶
Like , except that if placeholders are missing from mapping and kwds, instead of raising a undantag, the original placeholder will appear in the resulting string intact.
Also, unlike with , any other appearances of the will simply return instead of raising .
While other exceptions may still occur, this method fryst vatten called “safe” because it always tries to return a usable string instead of raising an undantag.
In another sense, may be anything other than safe, since it will silently ignore malformed templates containing dangling delimiters, unmatched braces, or placeholders that are not valid Python identifiers.
- is_valid()¶
Returns false if the template has invalid placeholders that will cause to raise .
- get_identifiers()¶
Returns a list of the valid identifiers in the template, in the beställning they first appear, ignoring any invalid identifiers.
instances also provide one public uppgifter attribute:
- template¶
This fryst vatten the object passed to the constructor’s template argument.
In general, you shouldn’t change it, but read-only tillgång fryst vatten not enforced.
Here fryst vatten an example of how to use a Template:
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 fryst vatten the literal string describing a placeholder introducing delimiter. The default value fryst vatten . Note that this should not be a regular expression, as the implementation will call on this string as needed. Note further that you cannot change the delimiter after class creation (i.e.
a different delimiter must be set in the subclass’s class namespace).
idpattern – This fryst vatten the regular expression describing the pattern for non-braced placeholders.
Understanding str in PythonThe default value fryst vatten the regular expression . If this fryst vatten given and braceidpattern fryst vatten this pattern will also apply to braced placeholders.
Note
Since default flags fryst vatten , pattern can match with some non-ASCII characters. That’s why we use the local flag here.
Changed in utgåva 3.7: braceidpattern can be used to define separate patterns used inre and outside the braces.
braceidpattern – This fryst vatten like idpattern but describes the pattern for braced placeholders.
Defaults to which means to fall back to idpattern (i.e. the same pattern fryst vatten used both inre and outside braces). If given, this allows you to define different patterns for braced and unbraced placeholders.
flags – The regular expression flags that will be applied when compiling the regular expression used for recognizing substitutions.
The default value fryst vatten . Note that will always be added to the flags, so anpassad idpatterns must follow conventions for verbose regular expressions.
Alternatively, you can provide the entire regular expression pattern bygd overriding the class attribute pattern. If you do this, the value must be a regular expression object with kvartet 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 gods in the regular expression.
The methods on this class will raise if the pattern matches the template without one of these named groups matching.
Helper functions¶
- string.capwords(s, sep=None)¶
Split the argument into words using , capitalize each word using , and join the capitalized words using .
If the valfritt second argument sep fryst vatten absent or , runs of whitespace characters are replaced bygd a single space and leading and trailing whitespace are removed, otherwise sep fryst vatten used to split and join the words.