Files
freeCodeCamp/guide/russian/python/string-methods/string-replace-method/index.md
2018-10-16 21:32:40 +05:30

41 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: String Replace Method
localeTitle: Метод замены строки
---
## Метод замены строки
Метод `str.replace(old, new, max)` используется для замены подстроки `old` на строку `new` для общего количества `max` . Этот метод возвращает новую копию строки с заменой. Исходная строка `str` не изменяется.
#### Примеры
1. Заменить все вхождения `"is"` на `"WAS"`
```python
string = "This is nice. This is good."
newString = string.replace("is","WAS")
print(newString)
```
Вывод
```python
ThWAS WAS nice. ThWAS WAS good.
```
2. Замените первые 2 вхождения `"is"` на `"WAS"`
```python
string = "This is nice. This is good."
newString = string.replace("is","WAS", 2)
print(newString)
```
Вывод
```python
ThWAS WAS nice. This is good.
```
#### Дополнительная информация:
Подробнее о замене строк в [документах Python](https://docs.python.org/2/library/string.html#string.replace)