All standard sequence operations apply to strings, but strings are immutable.
Character % is used for string formatting:
Put a string on the left side of %, and put the value to be formatted on the right side. You can use one value or the tuples or dictionaries of multiple values.
>>> format=>>> values=(,>>> format % ya?
If the tuples to be converted exist as part of the conversion expression, you must enclose them in parentheses to avoid errors.
String Method
Strings are much richer than list methods, because strings are inherited from the string module. This article only introduces some particularly useful string methods.
1. find
The find method can be used to search for a substring in a long string. It returns the leftmost index of the substring location. If no substring is found,-1 is returned.
>>> .find(7>>> title=>>> title.find(>>> title.find(6>>> title.find(-1
This method can accept the optional start and end parameters:
>>> subject=>>> subject.find(>>> subject.find(,120>>> subject.find(16>>> subject.find(,0,16-1
2. join
The join method is a very important string method. It is the inverse method of the split method, used to add elements to the queue:
>>> seq=[1,2,3,4,5>>> sep=>>>>>> seq=[,,,,>>>>>> dirs=,,,>>> >>> +
3. lower
The lower method returns the lower-case string.
>>>
4. replace
The replace method returns the string after all matching items of a string are replaced.
>>> .replace(,
5. split
It is the inverse method of join, used to split strings into Sequences
>>> .split(, , , , >>> .split(>>> , , ]
NOTE: If no separator is provided, the program uses all spaces as separators.
6. strip
The strip method returns a string that removes spaces on both sides (excluding internal spaces:
>>>
You can also specify the characters to be removed and column them as parameters:
>>> .strip(
Note: Only the characters on both sides are removed.
7. translate
The translate method is the same as the replace method. Some parts of the string can be replaced. However, unlike the former method, the translate method only processes a single character.