
文字列分割する関数「split/ splitlines」の説明をするよ
【split】
splitは対象の文字を分割する関数です。
文法(1)
変数名. split(“分割の区切り文字”)
コード例(1)
1text = “print, 出力関数”
2cnt , value = text.split(“,”)
3print(cnt +“は”+ value“です”)
実行結果(1)
printは出力関数です。
<リストに文字を入れる場合>
文法(2)
変数名. split(“分割の区切り文字”)
コード例(2)
1 text = “print, input, str”
2 a_list = text.split(“,”)
3 print (a_list)
実行結果(4)
[“print”,”input”,”str”]
【splitlines】
splitlinesは改行位置で文字列を分割する関数です。
文法
変数名. splitlines()
コード例(1)
1 text = “print\ninput\nstr”
2 a_list = text.splitlines()
3 print (a_list)
実行結果(1)
[“print”,” input”,” str”]
コード例(2)
1 text = “print\ninput\nstr”
2 a_list = text.splitlines(“\n”)
3 print (a_list)
コード(3)
1 text = “print\ninput\nstr”
2 a_list = text.splitlines()
3 print (a_list)
実行結果 (2) (3)
[“print”,” input”,” str”]