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)