Python String concatenation, truncation and Replacement Methods Summary analysis, python string

Source: Internet
Author: User
Tags python string concatenation

Python String concatenation, truncation and Replacement Methods Summary analysis, python string

This document describes how to splice, intercept, and replace Python strings. We will share this with you for your reference. The details are as follows:

Python string connection

There are several methods to connect python strings. The efficiency of the first method I used was the lowest. After reading the book, I used the following two efficient methods to share with you.

First, we will introduce the method with low efficiency:

a = ['a','b','c','d']content = ''for i in a:content = content + iprint content

The result of content is: 'abc'

Later, after reading the book, I found that the book talks about the python connection string, especially when looping the connection string, every time he connects, he needs to re-open the space and then connect the string, put the new space into the new space, re-loop, and open up new space, connect the string to the new space, so repeatedly, memory operations are frequent, and the memory space needs to be calculated each time, and then open up the memory space, and then release the memory space. The efficiency is very low. You may not be able to see it when you operate a small amount of data, however, when you encounter a large amount of data operations, this method is about to retire. Let's take a look at the following two advanced methods.

Advanced Method 1:

Use the string join method:

a = ['a','b','c','d']content = ''content = ''.join(a)print content

Result of content: 'abc'

Advanced Method 2:

Replace a placeholder with a string

a = ['a','b','c','d']content = ''content = '%s%s%s%s' % tuple(a)print content

The result of content is: 'abc'

If you are interested, you can check python string replacement and python string truncation related to python strings.

String Truncation

Python strings are ordered sets. We can use indexes to extract the desired characters. We can also use python strings as a list of strings for better understanding.

The python string list has two values.

1. The default value is 0 from left to right. The maximum value is 1 less String Length.

S = 'ilovepython'

S [0] results in I

2. The index is from right to left and starts from-1 by default. The maximum value is the start of the string.

S = 'ilovepython'

The result of s [-1] is n.

The above is to get a character. if you actually want to get a broken character, you can use the python string truncation function.

For example, s = 'ilovepython'

S [] returns love

When a string separated by colons is used, python returns a new object. The result contains the continuous content identified by the offset. The start of the left part contains the lower boundary, for example, the above result contains the value l of s [1], and the maximum obtained range does not include the upper boundary, that is, the value p of s [5 ].

String replacement

Replacement of python strings is a problem that is often encountered when python operates on strings. Here we will briefly introduce the replacement method of strings.

Python string replacement can be implemented in two ways:

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 restrinfo = re.compile('word')b = strinfo.sub('python',a)print b

The output is also hello python.

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.