* fix: converted single to triple backticks13 * fix: added prefix Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: removed language in wrong place Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: add language postfix Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: removed language in wrong place Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>
41 lines
1.1 KiB
Markdown
41 lines
1.1 KiB
Markdown
---
|
|
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. استبدال أول تكرارين لـ `"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) |