String
All standard frontal sequence operations (index, Shard, multiply, Judge membership, seek length, maximum minimum) apply to the string, but the string is immutable
>>> a = ' My name is Hahaahh ' >>> a[:3] = ' SDSD 'Traceback (most recent call last): File "<p Yshell#24> ", line 1, inch <module> a[:3] = ' SDSD 'TypeError: ' str ' object does not support item Assignme NT
String Format Strings-Lite version
String formatting applies to the string formatting operator, which is percent% to implement
--formatted string%s
>>> format = ' Hello,%s,%s enough for you ' >>> values = (' haha ', ' Zhang ') >>> print format%
Valueshello, Haha,zhang enough for
you
If you want to format a real number, use the%f
Formatted string--full version
Pass
String Method--to enumerate some particularly useful
Find-You can find substrings in a longer string, return the leftmost index where the substring is located, and return 1 if not found
>>> A = "Zhang is a good boy" >>> a.find ("is") 6>>> a.find ("Cheng") -1>>> " Zhang is a ". Find (" a ") 2
Join--Used to concatenate elements in a sequence (elements must all be strings), which is the inverse method of Split
>>>SEQ = ["1", "2", "3"]>>> Sep = "+" >>> sep.join (seq) ' 1+2+3 '
Lower--The lowercase master that returns a string
Replace--Returns the string after all occurrences of a string have been replaced
>>> ' This was a boy '. Replace (' was ', ' KK ') ' Thkk kk a boy '
Split--Splitting a string into a sequence is the inverse method of join
>>> ' 1+2+3+4+5 '. Split (' + ') [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 '] #如果不提供分隔符, then use a space as a separator >>> "I am a Boy". sp Lit () [' I ', ' am ', ' a ', ' Boy ']
Strip--Remove the spaces on both sides
Translate--can replace some parts of a string, but unlike replace, he only handles a single character
Python learning 2-using strings