how does string split work - Search
Open links in new tab
  1. 123

    The split() method is used to divide a string into an ordered list of substrings, places these substrings into an array, and returns the array. The division is done by searching for a specified pattern; the default is to split on any whitespace.

    Example in Python

    text = "welcome to the jungle"
    words = text.split()
    print(words) # Output: ['welcome', 'to', 'the', 'jungle']

    Specifying a Separator

    You can specify a separator to use for splitting. If no separator is specified, any whitespace string is considered a separator.

    Example with Separator

    txt = "apple#banana#cherry#orange"
    x = txt.split("#")
    print(x) # Output: ['apple', 'banana', 'cherry', 'orange']

    Limiting the Number of Splits

    The maxsplit parameter determines the number of splits to perform. The default value is -1, which means "all occurrences".

    Example with maxsplit

    txt = "apple#banana#cherry#orange"
    x = txt.split("#", 1)
    print(x) # Output: ['apple', 'banana#cherry#orange']

    Note: When maxsplit is specified, the returned list will contain the specified number of elements plus one.

    Continue reading
    Feedback
    Kizdar net | Kizdar net | Кыздар Нет