Python string operations Encyclopedia

Defined in Python string: a Unicode code point sequence consisting of immutable (Strings are immutable sequences of Unicode code points).

Python provides a powerful built-in functions to use string, master common method for data processing, interviews, written tests are very useful.

dir(str)['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__','__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

First, the string is a sequence, so the support sections and index operations, specifically reading this Python explain the basis of the data type .

The following is a summary of common usage of built-in functions:

  •  
s="hello testers !"print(s.replace('hello','hi')) #输出hi testers !

  •  
s="hello testers !"print(s.split()) #输出['hello', 'testers', '!']print(s.partition('testers')) #输出('hello ', 'testers', ' !')

  •  
s=" hellotesters! "print(s.strip()) #输出hellotesters!print(s.rstrip()) #输出 hellotesters!print(s.lstrip()) #输出hellotesters!

  •  
s1="hellotesters!"s2="HELLO TESTERS!"s3="Hello Testers!"s4=s1="hello testers!"print(s1.upper()) #输出HELLO TESTERS!print(s2.lower()) #输出hello testers!print(s3.swapcase()) #输出hELLO tESTERS!print(s1.capitalize()) #输出Hello testers!print(s4.title()) #输出Hello Testers!

  •  
s1="hello testers!"print(s1.count('l')) #输出2print(s1.find('hello')) #输出0print(s1.rfind('testers!')) #输出6

 

  • ​​​​​​​
s1="hellotesters!1234567890"s2="hellotesters"s3="1234567890"s4="HELLOTESTERS"print(s1.isalnum()) #输出Falseprint(s2.isalpha()) #输出Trueprint(s3.isdigit()) #输出Trueprint(s4.isupper()) #输出Trueprint(s2.islower()) #输出True
Published 101 original articles · won praise 73 · views 120 000 +

Guess you like

Origin blog.csdn.net/usstmiracle/article/details/104777113