Chapter 3 using strings

  1. You have learned how to use indexes and slices to access characters in strings
  2. Next, learn to format other values using strings
  3. Important tasks that can be accomplished using string methods, such as splitting, merging, and finding.

3.1 basic operation of string

All standard sequence operations (index, slice, multiplication, membership check, length, minimum and maximum) apply to strings, but don't forget that strings are immutable, so all element assignments and slice assignments are illegal.

3.2 format string: compact version

Method 1:

Specify a string (format string) to the left of% and the value to format on the right. When specifying a value to format, you can use a single value (such as a string or number), tuples (if you want to format multiple values), and dictionaries, the most common of which is tuples.

>>> format = "Hello, %s. %s enough for ya?" 
>>> values = ('world', 'Hot')
>>> format % values
'Hello, world. Hot enough for ya?'

%s is called a conversion specifier and indicates where to insert the value. s means that the value is formatted as a string. If the specified value is not a string, it is converted to a string using str. Other specifiers will result in other forms of conversion.

Method 2:

Template string. It uses a UNIX shell like syntax designed to simplify the basic formatting mechanism.

>>> from string import Template
>>> tmpl = Template("Hello, $who! $what enough for ya?") 
>>> tmpl.substitute(who="Mars", what="Dusty")
'Hello, Mars! Dusty enough for ya?'

When writing new code, you should choose to use the string method format, which combines and strengthens the advantages of earlier methods. When using this method, each replacement field is enclosed in curly braces, which may contain a name and may also contain information about how to convert and format the corresponding value.

# In the simplest case, the replacement field has no name or refers to the index as a name.
>>> "{}, {} and {}".format("first", "second", "third") 
'first, second and third'
>>> "{0}, {1} and {2}".format("first", "second", "third") 'first, second and third'
# Indexes do not need to be arranged in order as above.
>>> "{3} {0} {2} {1} {3} {0}".format("be", "not", "or", "to") 'to be or not to be'
# Named field. The order of keyword parameters is irrelevant
>>> from math import pi
>>> "{name} is approximately {value:.2f}.".format(value=pi, name="π") 
'π is approximately 3.14.'
# In Python 3.6, if the variable has the same name as the replacement field, a abbreviation can also be used. Use the f string -- precede the string with F.
>>> from math import e
>>> f"Euler's constant is roughly {e}."
"Euler's constant is roughly 2.718281828459045."
# This is equivalent to the following more explicit expression:
>>> "Euler's constant is roughly {e}.".format(e=e) 
"Euler's constant is roughly 2.718281828459045."

3.3 format string: full version


3.4 string method

There are too many string methods. Here are only some of the most useful.

3.4.1 center

The center method centers the string by adding padding characters (space by default) on both sides.

>>> "The Middle by Jimmy Eat World".center(39)
' The Middle by Jimmy Eat World '
>>> "The Middle by Jimmy Eat World".center(39, "*") 
'*****The Middle by Jimmy Eat World*****'
3.4.2 find

The find method finds a substring in a string. If found, the index of the first character of the substring is returned, otherwise - 1 is returned.
The string method find does not return a Boolean value. If find returns 0, it finds the specified substring at index 0.

>>> 'With a moo-moo here, and a moo-moo there'.find('moo') 
>>> title = "Monty Python's Flying Circus"
>>> title.find('Monty')
>>> title.find('Python')
>>> title.find('Flying') 
>>> title.find('Zirquss') 
# You can also specify the start and end points of the search (both optional).
>>> subject = '$$$ Get rich now!!! $$$' 
>>> subject.find('$$$')
>>> subject.find('$$$', 1) # Only the starting point is specified 
>>> subject.find('!!!')
>>> subject.find('!!!', 0, 16) # Both start and end points are specified
# The start and end values specify a search range that includes a start point but not an end point. This is Python's usual practice
3.4.3 join

join is the opposite of split. It is used to merge the elements of a sequence.
The elements of the merged sequence must all be strings.

>>> seq = [1, 2, 3, 4, 5]
>>> sep = '+'
>>> sep.join(seq) # Try to merge a number list Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: sequence item 0: expected string, int found 
>>> seq = ['1', '2', '3', '4', '5']
>>> sep.join(seq) # Merge a list of strings
>>> dirs = '', 'usr', 'bin', 'env'
>>> '/'.join(dirs)
>>> print('C:' + '\\'.join(dirs))
3.4.4 lower

The lower method returns a lowercase version of a string.

>>> 'Trondheim Hammer Dance'.lower()
'trondheim hammer dance'
# One method related to lower is title (see Appendix B). It converts a string to initial uppercase, that is, the first letter of all words is uppercase and the other letters are lowercase. However, the way it determines word boundaries may lead to unreasonable results.
>>> "that's all folks".title() 
"That'S All, Folks"
# Another way is to use the function capwords in the module string
>>> import string
>>> string.capwords("that's all, folks") 
That's All, Folks"
# Of course, you have to write your own code to realize real initial capitalization (articles, parallel conjunctions and prepositions no more than 5 letters may all be lowercase according to your writing style)
3.4.5 replace

replace replaces the specified substring with another string and returns the replaced result.

>>> 'This is a test'.replace('is', 'eez') 
'Theez eez a test'
3.4.6 split

Split is the opposite of join. It is used to split a string 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 specified, it will be split by default at one or more consecutive white space characters (spaces, tabs, line breaks, etc.)
3.4.7 strip

strip deletes the whitespace at the beginning and end of the string (but excluding the whitespace in the middle), and returns the deleted result.

>>> ' internal whitespace is kept '.strip() 
'internal whitespace is kept'
# You can also specify which characters to delete in a string parameter
>>> '*** SPAM * for * everyone!!! ***'.strip(' *!') 
'SPAM * for * everyone'
# This method only deletes the specified characters at the beginning or end, so the asterisk in the middle is not deleted

3.4.8 translate

translate replaces a specific part of a string just like replace, except that it can only replace a single character. The advantage of this method is that it can replace multiple characters at the same time, so it is more efficient than replace.

  1. A conversion table must be created before using translate. This conversion table indicates the conversion relationship between different Unicode code points.
  2. To create a conversion table, call the maketrans method on the string type str, which takes two parameters: two strings of the same length, which specify that each character in the first string is replaced with the corresponding character in the second string
>>> table = str.maketrans('cs', 'kz')
# If you like, you can view the contents of the conversion table, but all you see is the mapping between Unicode code points
>>> table
{115: 122, 99: 107}
# After you create a transformation table, you can use it as an argument to the method translate.
 >>> 'this is an incredible test'.translate(table)
'thiz iz an inkredible tezt'
# When you call the method maketrans, you can also provide an optional third parameter that specifies which letters to delete
>>> table = str.maketrans('cs', 'kz', ' ')
>>> 'this is an incredible test'.translate(table) 'thizizaninkredibletezt'
3.4.9 judge whether the string meets specific conditions

Many string methods start with is, such as isspace, isdigit and isupper. They judge whether the string has specific properties (such as all characters are blank, numeric or uppercase). These methods return True if the string has a specific property, otherwise False.

Keywords: Python

Added by yeshuawatso on Fri, 22 Oct 2021 11:49:04 +0300