str
10 methodsBuilt-in string type. Immutable sequence of Unicode code points.
str.split(sep=None, maxsplit=-1)Return a list of substrings split at sep. If sep is None, splits on any whitespace and discards empty strings.
Parameters
| Name | Type | Description |
|---|---|---|
| sep | str | None | Delimiter to split on. None splits on whitespace. |
| maxsplit | int | Maximum number of splits (-1 for no limit). |
Returns
list[str]
Example
'a,b,c'.split(',') # ['a', 'b', 'c']
' hello world '.split() # ['hello', 'world']
'a-b-c'.split('-', 1) # ['a', 'b-c']str.replace(old, new, count=-1)Return a copy with all occurrences of substring old replaced by new.
Parameters
| Name | Type | Description |
|---|---|---|
| old | str | Substring to replace. |
| new | str | Replacement string. |
| count | int | Maximum replacements (-1 for all). |
Returns
str
Example
'hello world'.replace('world', 'Python') # 'hello Python'
'a-b-c'.replace('-', '_', 1) # 'a_b-c'str.find(sub[, start[, end]])Return the lowest index where sub is found, or -1 if not found. Does not raise.
Parameters
| Name | Type | Description |
|---|---|---|
| sub | str | Substring to find. |
| start | int | Start index (inclusive). |
| end | int | End index (exclusive). |
Returns
int
Example
'hello world'.find('world') # 6
'hello'.find('z') # -1
'abcabc'.find('a', 1) # 3str.format(*args, **kwargs)Format the string using replacement fields {0}, {name}, etc. Predecessor of f-strings.
Parameters
| Name | Type | Description |
|---|---|---|
| *args | Any | Positional arguments for numbered fields. |
| **kwargs | Any | Keyword arguments for named fields. |
Returns
str
Example
'{} {}'.format('a', 'b') # 'a b'
'{name}={value}'.format(name='x', value=10) # 'x=10'
'{0:.2f}'.format(3.14159) # '3.14'str.join(iterable)Concatenate the strings in iterable, using the string as separator.
Parameters
| Name | Type | Description |
|---|---|---|
| iterable | Iterable[str] | Iterable of strings to join. |
Returns
str
Example
','.join(['a', 'b', 'c']) # 'a,b,c'
'-'.join('abc') # 'a-b-c'
' '.join(['hello', 'world']) # 'hello world'str.strip([chars])Return a copy with leading and trailing characters removed. Default removes whitespace.
Parameters
| Name | Type | Description |
|---|---|---|
| chars | str | None | Characters to remove (None = whitespace). |
Returns
str
Example
' hi '.strip() # 'hi'
'xxhelloxx'.strip('x') # 'hello'str.startswith(prefix[, start[, end]])Return True if the string starts with prefix.
Parameters
| Name | Type | Description |
|---|---|---|
| prefix | str | tuple[str, ...] | Prefix or tuple of prefixes to check. |
| start | int | Start index. |
| end | int | End index. |
Returns
bool
Example
'hello'.startswith('he') # True
'file.py'.startswith(('a', 'f')) # Truestr.endswith(suffix[, start[, end]])Return True if the string ends with suffix.
Parameters
| Name | Type | Description |
|---|---|---|
| suffix | str | tuple[str, ...] | Suffix or tuple of suffixes to check. |
| start | int | Start index. |
| end | int | End index. |
Returns
bool
Example
'hello'.endswith('lo') # True
'file.py'.endswith(('.py', '.js')) # Truestr.upper()Return a copy with all cased characters converted to uppercase.
Returns
str
Example
'hello'.upper() # 'HELLO'str.lower()Return a copy with all cased characters converted to lowercase.
Returns
str
Example
'HELLO'.lower() # 'hello'