# split()の使い方

Home String Methods

# split()の例1

Split a string into a list where each word is a list item:

    txt = "welcome to the jungle"x = txt.split()
    print(x)

# split()の定義と使い方

split()関数は、文字列をとします。 区切り文字を指定できます。デフォルトの区切り文字は空白です。

注意:maxを指定すると、リストには 指定された要素数に1を加えた数。

# split()の構文

string.split(separator, max)

# split()の引数

パラメータ 説明

separator オプション. Specifies the separator to use when splitting the string. Default value is a whitespace

max オプション. Specifies how many splits to do. Default value is -1, which is "all occurrences"

# split()の例2

Split the string, using comma, followed by a space, as a separator:

    txt = "hello, my name is Peter, I am 26 years old"x = txt.split(", ")
    print(x)

# split()の例3

Use a hash character as a separator:

    txt = "apple#banana#cherry#orange"x = txt.split("#")
    print(x)

# split()の例4

Split the string into a list with max 2 items:

    txt = "apple#banana#cherry#orange"# setting the max parameter 
    to 1, will return a list with 2 elements!x = txt.split("#", 1)
    print(x)

Home String Methods