Python3で文字列を条件指定で分解する方法
当記事では、Python3で文字列を分解してリストにする方法をご紹介いたします。
一文字ずつに分解
一文字ごとに分解するときは文字列をlist()でリストに変換します。
word = "Hello World"
#一文字ごとに区切る
print(list(word))
#['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']特定の文字列で分解
特定の文字列で区切る場合はstr.split(分解につかう文字列)とします
空白で分解
word = "Hello World"
#空白で区切る
print(word.split(" "))
#['Hello', 'World']/で分解
category = "Men/Tops/T-shirts"
#/で区切る
print(category.split("/"))
#['Men', 'Tops', 'T-shirts']カテゴリ: Python

関連するサービス
記事の筆者

AVILEN編集部
株式会社AVILEN

