Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit265cfce

Browse files
committed
Add string replace method with examples
1 parentd135e1a commit265cfce

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

‎docs/cheatsheet/manipulating-strings.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,21 @@ Counts the number of occurences of a given character or substring in the string
312312
>>> sentence.count('e',7)
313313
# 7
314314
```
315+
316+
##Replace Method
317+
318+
Replaces all occurences of a given substring with another substring. Can be optionally provided a third argument to limit the number of replacements. Returns a new string.
319+
320+
```python
321+
>>> text="Hello, world!"
322+
>>> text.replace("world","planet")
323+
# 'Hello, planet!'
324+
325+
>>> fruits="apple, banana, cherry, apple"
326+
>>> fruits.replace("apple","orange",1)
327+
# 'orange, banana, cherry, apple'
328+
329+
>>> sentence="I like apples, Apples are my favorite fruit"
330+
>>> sentence.replace("apples","oranges")
331+
# 'I like oranges, Apples are my favorite fruit'
332+
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp