Pythonで文字列処理

文字の分割

下記のような変数があった場合。

i = '1,000円 〜 3,500円'

splitで分割文字列を指定可能。また、分割した値はlistになる。

>>> print(i.split(' 〜 '))
['1,000円', '3,500円']

文字列から不要な文字を削除する

単一の文字の場合はreplaceを使う。ただし、複数の条件指定ができないので、条件を羅列することになる。

>>> i = '1,000円 〜 3,500円'
>>> list = i.split(' 〜 ')
>>> for i in list:
...     text = i.replace(',', '')
...     print(text)
...     text2 = text.replace('円', '')
...     print(text2)
...
1000円
1000
3500円
3500