*Memos:
- My post explains a string.
replace() can replace the zero or more old
substrings of a string with a new
substring from the left to the right as shown below:
*Memos
- The 1st argument is
old
(Required-Type:str
): *Memos:- It's the substring replaced with
new
. - Don't use
old=
.
- It's the substring replaced with
- The 2nd argument is
new
(Required-Type:str
): *Memos:- It's the substring replacing
old
. - Don't use
old=
.
- It's the substring replacing
- The 3rd argument is
count
(Optional-Default:-1
-Type:int
): *Memos:- It decides how many matched substrings are replaced.
- If it's
-1
, allold
substrings are replaced. - Don't use
count=
.
v = "It's very very good" print(v.replace('er', 'ER')) print(v.replace('er', 'ER', -1)) print(v.replace('er', 'ER', 2)) # It's vERy vERy good print(v.replace('er', 'ER', 1)) # It's vERy very good print(v.replace('very', 'VERY')) print(v.replace('very', 'VERY', 2)) # It's VERY VERY good print(v.replace('very', 'VERY', 1)) # It's VERY very good print(v.replace('', '--')) # --I--t--'--s-- --v--e--r--y-- --v--e--r--y-- --g--o--o--d-- # ↓↓ print(v.replace('', ' ')) # I t ' s v e r y v e r y g o o d # ↓ ↓↓ print(v.replace(' ', ' ')) # It's very very good # ↓↓ ↓ print(v.replace(' ', ' ')) print(v.replace('', '')) print(v.replace('er', 'ER', 0)) print(v.replace('very', 'VERY', 0)) print(v.replace('ER', 'ER')) print(v.replace('VERY', 'VERY')) print(v.replace('really', 'VERY')) # It's very very good
removeprefix() can remove the prefix of a string as shown below:
*Memos
- The 1st argument is
prefix
(Required-Type:str
): *Memos:- It's the prefix of zero or more characters.
- Don't use
prefix=
.
v = 'hello world' print(v.removeprefix('he')) # llo world print(v.removeprefix('hello ')) # world print(v.removeprefix('hello wo')) # rld print(v.removeprefix('HE')) print(v.removeprefix('')) print(v.removeprefix('abc')) # hello world
removesuffix() can remove the suffix of a string as shown below:
*Memos
- The 1st argument is
suffix
(Required-Type:str
): *Memos:- It's the suffix of zero or more characters.
- Don't use
suffix=
.
v = 'hello world' print(v.removesuffix('ld')) # hello wor print(v.removesuffix(' world')) # hello print(v.removesuffix('lo world')) # hel print(v.removesuffix('LD')) print(v.removesuffix('')) print(v.removesuffix('abc')) # hello world
join() can concatenate the zero or more elements of an iterable as shown below:
*Memos
- The 1st argument is
iterable
(Required-Type:iterable
). - Don't use
iterable=
.
v = ['one', 'two', 'three', 'four', 'five'] print(''.join(v)) # onetwothreefourfive print(' '.join(v)) # one two three four five print(' '.join(v)) # one two three four five print('-'.join(v)) # one-two-three-four-five print(' - '.join(v)) # one - two - three - four - five print('<->'.join(v)) # one<->two<->three<->four<->five
v = [] print('-'.join(v)) # Nothing
v = [0, 12, 345, 6789] print(''.join(map(str, v))) print(''.join([str(num) for num in v])) # 0123456789 print('-'.join(map(str, v))) print('-'.join([str(num) for num in v])) # 0-12-345-6789
Top comments (0)