I. Basic string operations
All standard sequence operations (indexing, partitioning, multiplication, determining membership, length, minimum value, and maximum value) are also applicable to strings. However, remember that the strings are immutable.
Ii. String formatting: Lite version
String formatting is implemented using the string formatting operator % Percent. Place a string (formatted string) on the left side of %, and the value to be formatted on the right side. You can use a value, such as a string or number, or use tuples or dictionaries of multiple values.
>>> format = "Hello. %s. %s enough for ya?">>> values = ('world', 'Hot')>>> print(format % values)Hello. world. Hot enough for ya?Note: If you use a class table or another sequence to replace the tuples, the sequence is interpreted as a value. Only tuples and dictionaries can format more than one value.
The % s part of the formatted string is called the conversion specifier, which marks the location where the conversion value needs to be inserted.
Iii. String formatting: Full Version
1. If the right operand is a tuples, each element is formatted separately, and each value requires a corresponding conversion specifier.
2. The basic conversion specifiers include the following parts.
(1) % character: mark the start of the conversion specifier.
(2) Conversion sign (optional):-indicates the left alignment; + indicates adding a plus or minus sign before the conversion value; "" (blank character) indicates retaining spaces before a positive number; 0 indicates that the conversion value is filled with 0 if the number of digits is not enough.
(3) Minimum field width (optional): The converted string must have at least the width specified by this value. If it is *, the width is read from the value tuples.
(4) Point (.) and precision value (optional): If the conversion is a real number, the precision value indicates the number of digits after the decimal point. If the conversion is a string, the number indicates the maximum field width. If it is *, the accuracy will be read from the tuples.
(5) Conversion Type
3.1 simple conversion
You only need to write the Conversion Type for simple conversion.
>>> print(format % values)Hello. world. Hot enough for ya?>>> 'Price of eggs: $%d' % 42'Price of eggs: $42'>>> 'Hexadecimal price of eggs: %x' % 42'Hexadecimal price of eggs: 2a'
3.2 field width and precision
The field width is the minimum number of characters retained by the converted value, and the precision (For numeric conversion) is the number of decimal places in the result, or (for String Conversion) is the maximum number of characters that can be included in the converted value.
These two parameters are all integers (first the field width and then the precision), separated by the dot. Although both are optional parameters, if only precision is given, it must contain a vertex number.
>>> from math import pi>>> '%10f' % pi' 3.141593'>>> '%10.2f' % pi' 3.14'>>> '%.2f' % pi'3.14'>>> '%.*s' % (5, 'Guido van Rossum')'Guido'
3.3 symbols, alignment, and 0 padding
Before the field width and precision value, you can place a "standard table", which can be zero, plus, minus, or space. 0 indicates that the number is filled with 0.
>>> '%010.2f' % pi'0000003.14'
Minus sign (-) is used for alignment values:
>>> '%-10.2f' % pi'3.14 '
Blank ("") means adding a space before a positive number.
>>> print(('% 5d' % 10) + '\n' + ('% 5d' % -10)) 10 -10
The plus sign (+) indicates that both positive and negative numbers indicate symbols.
>>> print(('%+5d' % 10) + '\n' + ('%+5d' % -10)) +10 -10Iv. String Method
The string method is much richer, because the string inherits many methods from the string module. Although the string method is entirely from the string module, this module also contains constants and functions that cannot be used as the string method. Below are some useful string constants.
(1) string. digits: A string with a number of 0-9.
(2) string. letters: string containing all letters (uppercase and lowercase)
(3) string. lowercase: string containing all lowercase letters
(4) string. printable: string containing all printable characters
(5) string. punctuation: string containing all punctuation
(6) string. uppercase: string containing all uppercase letters
1. find
The find method can search for substrings in a long string. It returns the leftmost index of the substring location. If not found,-1 is returned.
>>> 'With no moo-moo here, and a moo-moo there'.find('moo')8>>> title = "Monty Python"s Flying Circus">>> title.find('Monty')0>>> title.find('Python')6>>> title.find('Zirquss')-1The find method can also accept optional start and end points parameters:
>>> subject = '$$$ Get rich now!!! $$$'>>> subject.find('$$$')0>>> subject.find('$$$', 1)20>>> subject.find('!!!')16>>> subject.find('!!!', 0, 16)-1The range specified by the start and end values (the second and third parameters) contains the first index, but does not contain the second index.
2. join
The join method is used to add elements to a queue. All queue elements to be added must be strings.
>>> seq = ['1', '2', '3', '4', '5']>>> sep = '+'>>> sep.join(seq)'1+2+3+4+5'>>> dirs = '', 'usr', 'bin', 'env'>>> '/'.join(dirs)'/usr/bin/env'
3. lower
The lower method returns the lower-case string.
>>> 'Trondhe Hammer Dance'.lower()'trondhe hammer dance'
The title method is related to the lower method, which converts a string to the title-that is, the first letter of all words in upper case, while other letters in lower case.
>>> "that's all folks".title()"That'S All Folks"
4. replace
The replace method returns the string obtained after all matching items of a string are replaced.
>>> 'This is a test'.replace('is', 'eez')'Theez eez a test'
5. split
The split method splits strings into sequences.
>>> '1+2+3+4+5'.split('+')['1', '2', '3', '4', '5']>>> '/usr/bin/env'.split('/')['', 'usr', 'bin', 'env']>>> 'Using the default'.split()['Using', 'the', 'default']If no separator is provided, the program uses all spaces as separators (spaces, tabulation, line breaks, etc ).
6. strip
The strip method returns a string that removes spaces on both sides (excluding internal spaces.
>>> ' internal whitespace is kept '.strip()'internal whitespace is kept'
You can also specify characters to be removed and column them as parameters.
>>> '*** SPAM * for * everyone!!! ***'.strip(' *!')'SPAM * for * everyone'This method only removes characters on both sides, so the asterisks in the string are not removed.