strip() / lstrip() / rstrip()
文字列の前後(または片側)にある空白を除去します。
構文
文字列.strip() / 文字列.lstrip() / 文字列.rstrip()
使用例
print(' Python '.strip())実行結果
Python💡 実践的な使用例
line = ' 田中, 25, 東京 \n'
fields = [s.strip() for s in line.split(',')]
print(fields)CSVのような区切り文字付きデータを読み込むとき、split()の結果それぞれにstrip()をかけて余分な空白や改行を取り除くのはよくある組み合わせです。
関連トピック
文字列