LoginSignup
0
1

More than 1 year has passed since last update.

残プロ 第-7回 ~pythonで文字列操作~

Posted at

文字列strの操作

今回実装する操作は以下の2つです.

1.大文字で区切る ex)"EveryDay" → "Every" + "Day"
2.コンマ[,]で区切る ex)"one,two,three" → "one" + "two" + "three"

サンプル.py

文字列str型に対するメソッドsplitと正規表現モジュールreのfindall関数を使用します.
splitは区切り文字が含まれず,findallは含めることができます.

sample.py
import re

def separateStr(string):
    separated_comma = string.split(',')
    if type(separated_comma) == list:
        separated_upper = []
        for s in separated_comma:
            separated_upper.append(re.findall('[A-Z][a-z]+', s))
    else:
        separated_upper = re.findall('[A-Z][a-z]+', separated_comma)
    return separated_upper
0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1