0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonで「文字列を指定した区切り文字で分割してリストとして取得する(split, splitlines)」の動作を確認してみた

Posted at

概要

Pythonで「文字列を指定した区切り文字で分割してリストとして取得する(split, splitlines)」の動作を確認してみました。以下のページを参考にしました。

実装

以下のファイルを作成しました。

sample.py
print("My First Album".split())
print("  Next  Page  ".split())
print("Apple\tOrange\tLemon".split())
print("")
print("Orange,Lemon,Apple".split(","))
print("Red*-*Blue*-*Green".split("*-*"))
print("  Next  Page  ".split(" "))
print("")
print("Next Page".split(" "))
print("Next  Page".split(" "))
print("Next   Page".split(" "))
print(" Next Page ".split(" "))
print("")
print("A B C D E".split(" "))
print("A B C D E".split(" ", 1))
print("A B C D E".split(" ", 2))
print("A B C D E".split(" ", 3))
print("A B C D E".split(" ", 4))
print("A B C D E".split(" ", 8))
print("")
str1 = "Orange\nLemon\nApple"
print(str1)
print(str1.splitlines())
print("")
str2 = """\
Hello
My name is Yamada
Thank you"""
print(str2)
print(str2.splitlines())
print("")
str1 = "Orange\nLemon\nApple"
print(str1)
print(str1.splitlines(True))

以下のコマンドを実行しました。

$ python3 sample.py 
['My', 'First', 'Album']
['Next', 'Page']
['Apple', 'Orange', 'Lemon']

['Orange', 'Lemon', 'Apple']
['Red', 'Blue', 'Green']
['', '', 'Next', '', 'Page', '', '']

['Next', 'Page']
['Next', '', 'Page']
['Next', '', '', 'Page']
['', 'Next', 'Page', '']

['A', 'B', 'C', 'D', 'E']
['A', 'B C D E']
['A', 'B', 'C D E']
['A', 'B', 'C', 'D E']
['A', 'B', 'C', 'D', 'E']
['A', 'B', 'C', 'D', 'E']

Orange
Lemon
Apple
['Orange', 'Lemon', 'Apple']

Hello
My name is Yamada
Thank you
['Hello', 'My name is Yamada', 'Thank you']

Orange
Lemon
Apple
['Orange\n', 'Lemon\n', 'Apple']

まとめ

何かの役に立てばと。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?