4.1 It is illegal to modify a string in this way

All standard sequence operations (indexing, slicing, multiplication, membership checking, length, minimum, and maximum) apply to strings, but remember that strings are immutable, so all element assignments and slice assignments are illegal.

>>> site = 'http://www.python.org'
>>> site[-3:]
'org'
>>> site[-3:] = 'com'
Traceback (most recent call last):
  File "<stdin>", line 1.in <module>
TypeError: 'str' object does not support item assignment
>>> 
Copy the code

4.2 Common String methods

2 center

Centers a string by a certain width.

>>> "Hello python".center(40)
' Hello python '
>>> "Hello python".center(40."*")
'**************Hello python**************'
>>> "Hello python".center(40."#") 
Traceback (most recent call last):
  File "<stdin>", line 1.in <module>
TypeError: The fill character must be exactly one character long
>>> "Hello python".center(40."#") 
'##############Hello python##############'
>>> 
Copy the code

4.2.2 the find

The find method looks for substrings in a string. Returns the index of the first character of the substring if found, otherwise -1.

>>> 'Wow, Python is the best programming language'.find(Python)
Traceback (most recent call last):
  File "<stdin>", line 1.in <module>
NameError: name 'Python' is not defined
>>> 'Wow, Python is the best programming language'.find('Python')
5
>>> 'Wow, Python is the best programming language'.find('c++')      
-1
Copy the code

Specifies the start and end points of the search

>>> string = 'Wow, Python is the best programming language'
>>> string.find('best'.3.25)
19
>>> string.find('best'.25)   
-1
>>> 
Copy the code

Holdings join

Join is a very important string method that, as opposed to split, merges elements of a sequence.

>>> seq = [1.2.3.4.5]
>>> sep = '+'
>>> sep.join(seq) # Try to merge a list of numbers
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
'1 + 2 + 3 + 4 + 5'
>>> dirs = ' '.'usr'.'bin'.'env'
>>> '/'.join(dirs)
'/usr/bin/env'
Copy the code

4.2.4 spilt

It does the opposite of join and splits a string into a sequence.

>>> '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'
Copy the code

Note that if no delimiter is specified, the default is to split at one or more consecutive whitespace characters (Spaces, tabs, newlines, and so on).

4.2.5 the lower

The lower method returns a lower-case version of the string. One method related to lower is title (see Appendix B). It converts the string to uppercase, that is, all single words are capitalized and all other letters are lowercase. However, the way it determines word boundaries can lead to unreasonable results.

 "that's all folks".title()
"That'S All, Folks"Another approach is to use the function capwords in the module String.>>> import string
>>> string.capwords("that's all, folks")
That's All, Folks"
Copy the code

4.2.6 the replace

The replace method replaces each stator string with another string and returns the result.

 'This is a test'.replace('is'.'eez')
'Theez eez a test'
Copy the code

4.2.7 Determine whether the string meets specific conditions

Many string methods start with is, such as isspace, isDigit, and isupper, which determine whether a string has a specific property (such as containing all characters in whitespace, digits, or uppercase). These methods return True if the string has specific properties, False otherwise. Here are some examples of functions isalnum, isalpha, isDecimal, isDigit, isIdentifier, islower, isnumeric, isprintable, isspace, and istitle And isupper.

This is only a partial list of common methods, but there are many more functions for handling Python strings. The most detailed is the Official Python documentation

New content in the use of Python for handling strings will be updated here.