Two effective methods for replacing Python strings

Source: Internet
Author: User

Python string replacementThis is a problem that is frequently encountered when operating strings in Python. Here we will briefly introduce the string replacement method.
Python string replacement can be implemented in two ways:
1 is the method of using the string itself.
2. Replace strings with regular expressions.

 

The following example is used for an experiment:
A = 'Hello word'
I replaced the word in string a with python.
1 use the replace method of the string itself
A. Replace ('word', 'python ')
The output is Hello python.

2. replace with a regular expression:
Import re
Strinfo = Re. Compile ('word ')
B = strinfo. sub ('python',)
Print B
The output is also hello python.

If you use this method, it depends on your choice.
If you are interested, you can take a look at the python string truncation. Hope it will help you!

Reprinted Old Wang Python: http://www.cnpythoner.com

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.