YouTip LogoYouTip

Python3 String Split

# Python3.x Python3 split() Method [![Image 3: Python3 Strings](#) Python3 Strings](#) * * * ## Description The `split()` method splits a string into a list using a specified separator. It divides the string into substrings and returns a list containing these substrings. If the second parameter `num` is specified, it splits into `num+1` substrings. The `split()` method is particularly useful for breaking a string into multiple parts based on a specific separator. ## Syntax The syntax for the `split()` method: str.split(str="", num=string.count(str)) ## Parameters * `str` -- Separator, defaults to all whitespace characters, including spaces, newlines (`n`), tabs (`t`), etc. * `num` -- Number of splits. If this parameter is set, it splits into at most `maxsplit+1` substrings. The default is `-1`, meaning split all. ## Return Value Returns a list of the split strings. ## Example The following examples demonstrate the usage of the `split()` function: ## Example (Python 3.0+) #!/usr/bin/python3 str = "this is string example....wow!!!"print(str.split())# Default separator is space print(str.split('i',1))# Separator is 'i' print(str.split('w'))# Separator is 'w' The output of the above example is: ['this', 'is', 'string', 'example....wow!!!']['th', 's is string example....wow!!!']['this is string example....', 'o', '!!!'] The following example uses `#` as the separator and specifies the second parameter as `1`, returning a list with two elements. ## Example (Python 3.0+) #!/usr/bin/python3 txt = "Google##Taobao#Facebook"# Second parameter is 1, returns a list with two elements x = txt.split("#", 1)print(x) The output of the above example is: ['Google', '#Taobao#Facebook'] * * Python3 Strings](#)
← Java Arraylist AddCpp Examples Data Types β†’