The Python programming language is a powerful object-oriented computer programming language. Its Powerful advantages can help us implement many functional requirements in actual programming. Today, we will first introduce the basic content about Python characters.
1. Representation of Python characters
A string consisting of single or double quotation marks.
- “abc” \
- ‘def’
Represents a string, while "abc" + "def" are two strings connected together, the two are different. "Can be any long string in the middle
Ii. Python character operations
1. case-insensitive Conversion
- S. capitalize () # string s: uppercase letters
- S. lower () # lowercase for all
- S. upper () # convert all to uppercase
- S. swapcase () # case-insensitive swap
- Len (s) # obtain the string size
2. Search for substrings
- S. find (substring, [start [, end]), return index value, cannot find, return-1
- S. rfind (substring, [start [, end]) Reverse Lookup
- S. index (substring, [start [, end]) is similar to find (). If substring cannot be found,
- ValueError exception
- S. rindex (substring, [start [, end]) Reverse Lookup
- S. count (substring, [start [, end]) returns the number of times the substring is found.
3. format the string
Usage: s % <tuple> tuple indicates a parameter list. Each value in the tuple is represented by a string, and the format represented is determined by s.
- S. ljust (width) is left aligned. If width is greater than len (s), spaces are added. Otherwise, return s.
- S. Align ust (width) right alignment
- S. center (width) center
- S. lstrip () removes the white space on the left
- S. rstrip () removes the white space on the right
- S. lstrip () removes spaces on both sides
4. Merge and decompose Python characters
Merge: s. join (words)
Words is a tuple or list containing strings. Join concatenates strings in words into a string using the separator "s.
Example:
- >>> “+”.join([”hello”,”my”,”friedn”])
- ‘hello+my+friedn’
Decomposition:
- s.split(words)
Words is a string that represents a separator. The split operation is opposite to the join operation. Splits s into a list.
Example:
- >>> “hello my fried”.split(” “)
- [’hello’, ‘my’, ‘fried’]
The preceding section describes the Python characters.