> For the complete documentation index, see [llms.txt](https://olexsyn.gitbook.io/enote/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://olexsyn.gitbook.io/enote/progr/python/list/join-split.md).

# join(), split()

<https://docs.python.org/3/library/stdtypes.html>

## join() - метод

*str* = "`D`".**join(***list/tuple***)**

Метод `join()` приймає всі елементи в ітеруючому об’єкті та об’єднує їх в один рядок, розділяючи роздільником `D`. Елементи повинні бути строкового типу ***str***!

```python
my_tuple = ("John", "Peter", "Vicky")
x = "-".join(my_tuple)
print(x)          # 'John-Peter-Vicky'
```

## split() - метод

*list* = *str*.**split(**"`D`", *maxsplit***)**

Розділяє рядок на частини по роздільнику `D` , повертає список частин:

```python
txt = "welcome to the jungle"
x = txt.split()
print(x)          # ['welcome', 'to', 'the', 'jungle']
y = txt.split('o')
print(y)          # ['welc', 'me t', 'the jungle']
z = txt.split(' ', 1)
print(z)          # ['welcome', 'to the jungle']
```

див.також `.rsplit()`, `.partition()`, `.splitlines()`,

## rsplit()

*list* = *str*.**rsplit(**"`D`", *rightmaxsplit***)**

Аналогічно `split()`, тільки *rightmaxsplit* рахується зправа

```python
txt = "Perl Java Python"
x = txt.rsplit()
print(x)          # ['Perl', 'Java', 'Python']
z = txt.split(' ', 1)
print(z)          # ['Perl Java', 'Python']
```

## partition()

*list\_length\_3* = *str*.**partition(**"`D`"**)**

Розбиває рядок на три частини по роздільнику. Повертає **кортеж** з трьох елементів: частина рядка до роздільника, роздільник, частина рядка після роздільника. Якщо роздільник не знайдений - дивися приклад:

```python
txt = "7.40"

x = txt.partition(".")
print(type(x))         # <class 'tuple'>
print(x)               # ('7', '.', '40')

x = txt.partition(",")
print(x)               # ('7.40', '', '')

```

## splitlines()

*list* = *str*.**splitlines(***keepends=False***)**

Метод `splitlines()` розбиває рядок на список. Поділ виконується на розривах рядків. З `True` зберігає розриви рядків у елементах списку.

```python
txt = "Thank you for the music\nWelcome to the jungle"

x = txt.splitlines()
print(x)           # ['Thank you for the music', 'Welcome to the jungle']

x = txt.splitlines(True)
print(x)           # ['Thank you for the music\n', 'Welcome to the jungle']

x = txt.splitlines(False)
print(x)           # ['Thank you for the music', 'Welcome to the jungle']
```

<table data-header-hidden><thead><tr><th width="279"></th><th></th></tr></thead><tbody><tr><td>Representation</td><td>Description</td></tr><tr><td><code>\n</code></td><td>Line Feed</td></tr><tr><td><code>\r</code></td><td>Carriage Return</td></tr><tr><td><code>\r\n</code></td><td>Carriage Return + Line Feed</td></tr></tbody></table>

та інші... (див <https://docs.python.org/3/library/stdtypes.html#str.splitlines>)
