Python string manipulation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.