2018/02/08

2020/04/14

Python3で文字列を条件指定で分解する方法

Python

ライター:

当記事では、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']

 

(totalcount 3,858 回, dailycount 7回 , overallcount 16,431,046 回)

ライター:

Python

single-banner

COMMENT

コメントを残す

メールアドレスが公開されることはありません。
*は必須項目です。




CAPTCHA