The first thing to know about a string as a common class is that it is not modifiable.
This means that no matter what method it comes in with, it cannot be modified by itself.
Take a look at the following example ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
I have defined a string named message, which I use
The method of the string lower () gets a single word from each
The parent becomes a lowercase string and the result is the last line. See
The string is changed, but it's not. Just created a
A new copy .
We can see that the message does not actually change , this
I was puzzled for a long time before I understood it anyway,
Once a string is created, it cannot be modified.
For example, we assign a new string value to the message,
Seems to have changed the characters before actually, just recycled
Memory of the previous string, and then opened up a new memory
Store "Hello, Clark" and point to it with a message.
So to get a new character for the string using its own method
String, and will use it later, it must
To have a variable can undertake this value, otherwise this new word
The memory of the copy of the string will be recovered immediately after creation, no longer exists.
So what are the methods of commonly used strings?
1. Title () → Capitalize the first letter of all the words in the string
2. Upper () → capitalize each letter in a string
3. lower () → Lowercase each letter in a string
4. Capitalize () → Capitalize the first character in a string (not modified if it starts with a letter)
← If the first character is a line break, there is no modification
← If it's a letter, turn it into uppercase
5. Find (< string >) → check if the string contains a string,
1. If included: Returns the string to be searched in the original word
The first occurrence of a string of characters from left to right
Value ( starting from 0 )
2. If not included: return-1
6. Index (< string >) → identical to the function of find, the only difference is that if you do not include
Instead of a return value, the error is thrown directly.
7. Istitle () → Determine if the string is a header
The so-called title to meet a few conditions: 1. Contains at least one letter
2. Capitalize the first letter of each word
8. Lstrip () → Remove the whitespace at the beginning of the left end (line break, space, etc.), you can also pass the parameters to remove
A specific character.
← The case of non-transfer of parameters
← Information about the parameters of the transfer
9. Rstrip () → Ditto, except for the right-hand character
Strip () → Ditto, except that the characters are removed from both ends
Split () → splits a string with a specific flag as a list, the default is a space, or you can pass a parameter designation.
Note that the character as a flag will not be present in the list at the end
← Conditions without parameters
← The case with parameters
Replace (< replaced string >,< substituted string >) → replaces part of a character string,
If there is more than one string fragment that matches, all will be replaced.
The above is part of what I remember, more ways to see it in the Python interpreter with help (STR)!
"Python Self-Study" (1) ———— string