"Python CookBook2" chapter I text-changing the indentation && expansion and Compression tabs of multiline text strings (this section is pending)

Source: Internet
Author: User

to change the indentation of multiple lines of text strings

Task:

There is a string that contains multiple lines of text, and you need to create a copy of the string. and add or remove some spaces at the beginning of each line to ensure that each line is indented with a specified number of spaces.

Solution:

#-*-coding:utf-8-*-" "Created on August 29, 2014 Path:e:\se\core_python\[email protected]: administratorfunction: Changing the indentation of multiline text strings" "defreindent (s,numspaces): Leading_space= Numspaces *' 'Lines= [Leading_space + Line.strip () forLineinchS.splitlines (True)]#return '. Join (lines)    return '\ n'. Join (lines)if __name__=="__main__": x=" "Hello python I Love" "    PrintReindent (x,4)    Pass" "splitlines in Python is used to split rows. When the passed-in parameter is true, a newline character is reserved \ n. It is clear from the following example that 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)) Run 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! " "

Extending and Compressing tabs

Task:

Converts a tab in a string to a certain number of spaces, or to the opposite.

Solution:

"Python CookBook2" chapter I text-changing the indentation && expansion and Compression tabs of multiline text strings (this section is pending)

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.