Pythonk BASICS (1) -------- string operations, pythonk Basics
1. Common Operations on strings:
Known string: str = "hello world zhangsan and zhangsan"
1. String Length: len (str)
2. view the index value of the string:
Str. find ("zhangsan", [start Index], [end Index]) by default, the query starts from the left and returns the index within the index range; otherwise,-1 is returned.
Str. rfind ("zhangsan", [start Index], [end Index.
Str. index ("zhangsan", [start index], [end index]) queries from the left by default and returns the index. Otherwise, an exception is thrown.
Str. rindex ("zhangsan", [start Index], [end Index]) Start from the right, as shown in the preceding figure.
3. view the number of times a character appears:
Str. count ("zhangsan", [start Index], [end Index]) view the number of times that zhangsan appears in [start Index] and [end Index.
4. Replace:
Str. replace ("zhangsan", "lisi", [count]) by default, but you can also specify the number of replicas [count].
5. Split the string:
Str1 = "a, B, B, c, d, e"
Str1.partition ("B") returns a list of [parts before B, parts after B]
Str1.rpartiton ("B") is the same as above, except that this function is split from the rightmost B.
Splitlines () are separated by rows. a list containing elements of each row is returned.
Str1.split (",") is a comma-separated string that returns a list.
6. Split the list in some way and convert it into a string:
In [91]: a = ["zhao","qian","sun","li"]In [92]: "_".join(a)Out[92]: 'zhao_qian_sun_li'
7. Remove blank space:
It only removes the leading and trailing spaces (line breaks, indentation characters) of the string, and does not include the trailing spaces in the string.
Str. strip () removes leading and trailing Spaces
Str. lstrip () removes the left blank
Str. rstrip () removes the white space on the right
8. Judgment:
Str. isalpha () determines whether it only contains letters
Str. isdigit () determines whether only numbers are included
Str. isalnum () determines whether it only contains numbers and letters
Str. isspace () determines whether it only contains spaces
9. Case Sensitive operations:
Str. capitalize () capital the first letter of the string
Str. title () string in uppercase
Str. lower () string to lowercase
Str. upper () All converted to uppercase
10. Other operations:
Str. startswith ("hello ")
Str. endswith ("zhangsan ")
Str. center (40) string centered, 40 characters in total, not enough space to fill
Str. ljust (40) string left alignment, same as above.
Str. Align ust (40) Right-aligned string, same as above.