python: notes on str.split()
This commit is contained in:
parent
baf34f6b57
commit
d09bc82729
2 changed files with 37 additions and 1 deletions
|
@ -13,7 +13,7 @@ tags: [python, data-types]
|
||||||
|
|
||||||
The core data-types are as follows:
|
The core data-types are as follows:
|
||||||
|
|
||||||
- str
|
- [str](/Programming_Languages/Python/Syntax/Strings_in_Python.md)
|
||||||
- bool
|
- bool
|
||||||
- float
|
- float
|
||||||
- double
|
- double
|
||||||
|
|
|
@ -76,3 +76,39 @@ print('sub string: ', 'Hello-World'[1:5])
|
||||||
user_age = input("Please enter your age: ")
|
user_age = input("Please enter your age: ")
|
||||||
print(f'You are {user_age}')
|
print(f'You are {user_age}')
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## `str.split()`
|
||||||
|
|
||||||
|
The `split()` function in Python is used to divide a string into multiple parts at the occurrence of a given separator. This function returns a [list](/Programming_Languages/Python/Syntax/Lists_in_Python.md) of substrings.
|
||||||
|
|
||||||
|
### General syntax
|
||||||
|
|
||||||
|
```py
|
||||||
|
str.split(separator, maxsplit)
|
||||||
|
```
|
||||||
|
|
||||||
|
- The `separator` is optional. It specifies the separator to use when splitting the string. If no separator is provided, it will default to using whitespace.
|
||||||
|
|
||||||
|
- `maxsplit` is also optional and specifies how many splits to do. Default value is -1, which is "all occurrences".
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
|
||||||
|
```py
|
||||||
|
|
||||||
|
text = "Hello world, how are you?"
|
||||||
|
|
||||||
|
# Default usage:
|
||||||
|
x = text.split()
|
||||||
|
print(x)
|
||||||
|
# ['Hello', 'world', 'how', 'are', 'you?']
|
||||||
|
|
||||||
|
# Using a specific separator
|
||||||
|
x = text.split(",")
|
||||||
|
print(x)
|
||||||
|
# ['Hello', 'world how are you?']
|
||||||
|
|
||||||
|
# Specifiying a maxplit value
|
||||||
|
x = text.split(" ", 1)
|
||||||
|
print(x)
|
||||||
|
# ['Hello']
|
||||||
|
```
|
||||||
|
|
Loading…
Add table
Reference in a new issue