ホームページ  >  記事  >  バックエンド開発  >  Pythonの分割線の使い方スキルを詳しく解説

Pythonの分割線の使い方スキルを詳しく解説

巴扎黑
巴扎黑オリジナル
2017-05-21 18:50:471992ブラウズ

Python の分割線は行を分割するために使用されます。渡されたパラメータが True の場合、改行文字 n が保持されることを意味します。次の例から明らかです

コードは次のとおりです:

mulLine = """Hello!!! 
Wellcome to Python's world! 
There are a lot of interesting things! 
Enjoy yourself. Thank you!""" 
print ''.join(mulLine.splitlines()) 
print '------------' 
print ''.join(mulLine.splitlines(True))


出力結果:

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

この関数を使用すると、インデントやその他のメソッドの処理など、いくつかの段落処理関数を記述するのに非常に便利です。クックブックの例:

コードは次のとおりです:

def addSpaces(s, numAdd): 
white = " "*numAdd 
return white + white.join(s.splitlines(True)) 
def numSpaces(s): 
return [len(line)-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)))

以上がPythonの分割線の使い方スキルを詳しく解説の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。