2018-10-12 16:35:31 -04:00
---
title: String Replace Method
localeTitle: طريقة استبدال السلاسل
---
## طريقة استبدال السلاسل
يتم استخدام `str.replace(old, new, max)` لاستبدال السلسلة الفرعية `old` بالسلسلة `new` لمجموع مرات `max` . هذا الأسلوب بإرجاع نسخة جديدة من السلسلة مع الاستبدال. لا يتم تغيير `str` السلسلة الأصلية.
#### أمثلة
1. استبدال كافة تكرارات `"is"` بـ `"WAS"`
2019-06-20 16:07:24 -07:00
```python
string = "This is nice. This is good."
newString = string.replace("is","WAS")
print(newString)
```
2018-10-12 16:35:31 -04:00
انتاج |
2019-06-20 16:07:24 -07:00
```python
ThWAS WAS nice. ThWAS WAS good.
```
2018-10-12 16:35:31 -04:00
2. استبدال أول تكرارين لـ `"is"` بـ `"WAS"`
2019-06-20 16:07:24 -07:00
```python
string = "This is nice. This is good."
newString = string.replace("is","WAS", 2)
print(newString)
```
2018-10-12 16:35:31 -04:00
انتاج |
2019-06-20 16:07:24 -07:00
```python
ThWAS WAS nice. This is good.
```
2018-10-12 16:35:31 -04:00
#### معلومات اكثر:
اقرأ المزيد حول استبدال السلسلة في [مستندات Python ](https://docs.python.org/2/library/string.html#string.replace )