Python string goes to the middle space __python

Source: Internet
Author: User

Because of the effects of other languages, some places are still not used to it.

A truncated string is one example.

Left, right, first no longer explained, because the function can be used directly.

The main record of the middle space processing.

Method 1:

' ... \0....\0 ... '. Replace (' the ', ')

This is not very good, not only because of the coding problem (2.x version of the use of repr () can see the space corresponding to the encoding, with its replacement), and too cumbersome, inflexible


Method 2:

Strip (), if this method can work directly, it will save a lot of trouble, but the effect, who knows. In most cases, it doesn't work.

Method 3:

Split first, then join.

s = ' China Tall program Ape is purely ... '
x = S.split (')
y = '. Join (x)

It seems to be troublesome, but very effective;

This can also be written in Jane:

'. Join ([x for x in S if x!= "])


On and off, hehe, very in accordance with the natural law Ah,


If there is a more agile, more convenient way to achieve, please leave a message.

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.