3 use string

Keywords: Python

catalogue

3.4 string method

But the string is not dead

3.4.1 find method

3.4.2 join

3.4.3 lower

Title Conversion

Next section: replace, split, strip, translate, etc

3.4 string method

String "inherits" many methods from the string module. In early python versions, these methods appeared as functions.

But the string is not dead

Although the string method comes entirely from the string module, this module also includes constants and functions that cannot be used as string methods. Maketrans function is one of them, which will be introduced together with the translate method later. Here are some useful string constants.

(1)string.digits: Contains the number 0~9 String of('0123456789')

(2)string.letters: A string containing all letters (uppercase or lowercase)

(3)string.lowercase: A string containing all lowercase letters

(4)string.printable: A string containing all printable characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c')

(5)string.punctuation: A string containing all punctuation('!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')

(6)string.uppercase: A string containing all uppercase letters

Alphabetic string constants (such as string.letters) are region dependent (that is, their specific values depend on the language configured by python. string.letters and related contents in Python 3.0 will be removed. If necessary, string.ascii_letters constants should be used instead).

>>> import string

>>> print(string.letters)

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

The following is the case of Python 3

>>> import string

>>> string.letters   #python3 string.letters   report errors

Traceback (most recent call last):

  File "<pyshell#2>", line 1, in <module>

    string.letters

AttributeError: module 'string' has no attribute 'letters'

>>> string.ascii_letters 

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

Ps: similarly, string. Ascii_lowercase and string. Ascii_uppercase

3.4.1 find method

The find method can find a substring in a long string. It returns the leftmost index of the location of the substring. If it is not found, it returns - 1.

>>> "With a moo-moo here, and a moo-moo there".find('moo')
7
>>> title = "Monty Python's Flying Circus"
>>> title.find('Monty')
0
>>> title.find('Python')
6
>>> title.find('Flying')
15
>>> title.find('Zirquss')
-1

We first learned about membership in Chapter 2. We used the '$$$' expression in the subject to establish a spam filter. You can also use the find method (it is also available in versions before Python 2.3, but the in operator can only be used to find a single character in the string?):

>>> subject = '$$$ Get rich now!!! $$$'
>>> subject.find('$$$')
0
>>> '$$$' in subject
True

Note: the find method in the string does not return a Boolean value. If 0 is returned, it proves that a substring is found at index 0.

This method can also receive optional start and end point parameters:

>>> subject = '$$$ Get rich now!!! $$$'
>>> subject.find('$$$')
0
>>> subject.find('$$$', 1) #Only the starting point is provided
20
>>> subject.find('!!!')
16
>>> subject.find('!!!', 0, 16) #Provide start and end points
-1

Note that the range specified by the start and end values (second and third parameters) contains the first index, but not the second index. This is a convention in python.

Appendix B: rfind, index, rindex, count, startswitch, endswitch

3.4.2 join

The join method is a very important string method. It is the inverse of the split method and is used to connect the elements in the sequence

>>> seq = [1, 2, 3, 4, 5]
>>> sep = '+'
>>> sep.join(seq) #Connection number list
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, int found
>>> seq = ['1', '2', '3', '4', '5']
>>> sep.join(seq) #Connection string list
'1+2+3+4+5'
>>> dirs = '', 'usr', 'bin', 'env'
>>> '/'.join(dirs)
'/usr/bin/env'
>>> print 'C:' + '\\'.join(dirs)
C:\usr\bin\env
>>> print 'C:' + '\'.join(dirs)
  File "<stdin>", line 1
    print 'C:' + '\'.join(dirs)
                              ^
SyntaxError: EOL while scanning string literal

You can see that the sequence elements to be connected must be strings. Note that the list of directories is used in the last two examples, and different separator symbols are used in the format according to the conventions of UNIX and DOS/Windows (the driver name is also added in DOS version)

See: split

3.4.3 lower

(the template string import string is preceded by string.lowercase, and 3.0 is ascii_lowercase)

>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'    #Look back

The lower method returns a lowercase version of the string

>>> 'Trondheim Hammer Dance'.lower()
'trondheim hammer dance'

If you want to write "case insensitive" code, this method comes in handy - the code will automatically ignore the case status. For example, if you want to find out whether a user name exists in the list: the list contains the string Gumby, and the user enters' Gumby ', you can't find it.

>>> if 'Gumby' in ['gumby', 'smith', 'jones']: print 'Found it!'
... 
>>>

If 'Gumby' is stored and the user enters' Gumby 'or even' Gumby ', the result is the same. The solution is to convert all names to lowercase during storage and search. As follows:

>>> name = 'Gumby'
>>> names = ['gumby', 'smith', 'jones']
>>> if name.lower() in names: print 'Found it!'
... 
Found it!
>>> if 'Gumby'.lower() in ['gumby', 'smith', 'jones']: print 'Found it!'   #Direct a statement
... 
Found it!

See: translate
Appendix B: islower, capitalize, swapcase, title, istitle, upper, isupper

Title Conversion

Related to the lower method is the Title Method (see Appendix B), which converts the string into a title - that is, the first letter of all words is uppercase, while other letters are lowercase. However, the word division method it uses may get unnatural results:

>>> "that's all folks".title()
"That'S All Folks"

Then introduce the capwords function of another string module:

>>> import string
>>> string.capwords("that's all folks")
"That's All Folks"

Of course, if you want to get the correct capitalized title (depending on your style, you may need lowercase articles, conjunctions and prepositions less than 5 letters), you still have to grasp it yourself.

Next section: replace, split, strip, translate, etc

Posted by vigour on Wed, 01 Dec 2021 20:35:20 -0800