pythonで文字列を区切る方法/splitメソッドの使い方

divorce IT

splitメソッドを使い、文字列を任意の位置で区切る方法です。

スポンサーリンク

splitメソッド

普通にprint("abcde")を実行すると、文字列がそのまま返りますが、
#python
print("abcde".split("c"))
ここで文字列の後ろにsplitメソッドおよび区切る文字を指定してやると、
文字列から区切る文字が消滅し、その前後の文字列が分割して表示されます。
区切る文字の数だけ文字列は分割されます。

引数を指定しない場合

引数を指定しない場合、splitは半角スペースで文字を区切ります。
print("ab cd e").split()) → ‘ab’, ‘cd’, ‘e’を返す)
Python操作方法、関数・メソッドリファレンス
使用用途別にまとめたPythonリファレンスページです。 初期準備、基本概念 ダウンロード、インストール、起動 ShellとEditor、保存、実行 保存したPythonの起動 コメント、docstring、行またぎ コメント、...

コメント

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny

モバイルバージョンを終了