1. String connection
#JoinA = ['a','b','C','D']content="'. Join (a)#' ABCD '#Replace placeholderTest_str ='Hello%s! Welcome to%s'%('Tom','Our home')#tupleContent ='%s%s%s%s'%tuple (a)#ABCD#formatTest_str ='Hello% (name) s! Welcome to% (addr) s'D= {'name':'Tom','Addr':'Our home'}test_str%Dtest_str='Hello {name}! Welcome to {addr} {0}'Test_str.format ('^^', name='Tom', addr='Our home')
2. String substitution
A ='Hello Word'b= A.replace ('Word','python')PrintB#Hello pythonImportRea='Hello Word'Strinfo= Re.compile ('Word') b= Strinfo.sub ('python', a)PrintB#Hello python
3. String inversion
' ABCD ' = a[::-1]##[::-1] via step reversal print= list (a) b.reverse () ". Join (b)
4. Encoding
a = " Hello b = " Python " print A.decode ( ' ) # #decode方法把字符串转换为unicode对象
5. Splitting
A='helloworld '# [' Hello ', ' world ']
Python string manipulation