Strings are a very important type of data, and today we'll look at the inside of str together.
1. Declaration of a string
Myname="Ben"yourname=STR ("Jin") Print ("the value of MyName is%s and the type is%s"%(Myname,type (myname))) print ("the value of Yourname is%s and the internal member is%s"%(Yourname,dir (myname)))==========================================return Result: The value of MyName is Ben, type<class 'Str'>the value of Yourname is Jin, and the internal member is ['__add__','__class__','__contains__','__delattr__','__dir__','__doc__','__eq__','__format__','__ge__','__getattribute__','__getitem__','__getnewargs__','__gt__','__hash__','__init__','__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','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']
2. __init__ Construction Method Yourname=str ("Jin") is __init__ when the object is generated.
Does the 3.__contains__ contain
Myname="Ben"result=myname.__contains__ ("en" ) ) print (result)=========================== return result: True
4.__GETATTRIBUTE__ reflection is called unconditionally, accessing properties through an instance. If __getattr__ () is defined in class, __getattr__ () is not called (unless the call is displayed or the Attributeerror exception is thrown)
5. __getitem__ Dic[1] Actually implemented is the __getitem__ method of the dictionary
6. Capitalize initial letter case
Myname="ben"result=myname.capitalize () print (result)============== ===================== running results Ben
7. Casefold is similar to lower, where the string becomes lowercase, but not just for ASCII, but also to lowercase some German. Chinese & English Environment under, continue to use lower() no problem; To work with other languages and when case is presentcasefold()
8. Center centers the string and surrounds it with other characters
Myname="ben"result=myname.center ("*") ) print (result)=========================== return result:********ben*********
9. Count counts the number of occurrences of a part of a string
Myname="benbenben"result=myname.count ("ben", 2,9) print (result)========================= return Result:2
Endwith decide whether to end with a string
Myname="benbenben"result=myname.endswith ("ben", 2,9) print (result)============================== return result: True
Encode.
Myname=" Chen " ##utf8result=myname.encode ("GBK " # #内部是先转成unicode then turn into GBK . Print (Result)================================= return result: B'\xb3\xc2\xd5\xe6 '
Python string str