Python splitlines Usage Tips

Source: Internet
Author: User
Copy CodeThe code is as follows:


Mulline = "" "Hello!!!
Wellcome to Python ' s world!
There is a lot of interesting things!
Enjoy yourself. Thank you! "" "

print '. Join (Mulline.splitlines ())
print '------------'
print '. Join (Mulline.splitlines (True))


Output Result:
Hello!!! Wellcome to Python ' s world! There is a lot of interesting things! Enjoy yourself. Thank you!
------------
Hello!!!
Wellcome to Python ' s world!
There is a lot of interesting things!
Enjoy yourself. Thank you!

Using this function, it is very convenient to write some of the functions of the paragraph processing, such as processing indentation and other methods. Examples in the cookbook book:

Copy CodeThe code is as follows:


def addspaces (S, numadd):
White = "" *numadd
return white + white.join (S.splitlines (True))
def numspaces (s):
return [Len]-len (Line.lstrip ()) for line in S.splitlines ()]
def delspaces (S, Numdel):
If Numdel > Min (numspaces (s)):
Raise ValueError, "removing more spaces than there are!"
Return ' \ n '. Join ([Line[numdel:] for line in S.splitlines ()])
def unindentblock (s):
Return delspaces (S, Min (numspaces (s)))
  • 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.