Hướng dẫn how do i strip all spaces from a string in python? - làm cách nào để loại bỏ tất cả khoảng trắng khỏi chuỗi trong python?

Các kỹ thuật tiêu chuẩn để lọc một danh sách áp dụng, mặc dù chúng không hiệu quả như các phương pháp

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
3 hoặc
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
4.

Chúng tôi cần một bộ không gian trắng:

>>> import string
>>> ws = set(string.whitespace)

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
5 tích hợp:

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'

Danh sách hiểu (có, sử dụng dấu ngoặc: xem điểm chuẩn bên dưới):

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'

Không biết:

>>> import functools
>>> "".join(functools.reduce(lambda acc, c: acc if c in ws else acc+c, "strip my spaces"))
'stripmyspaces'

Benchmark:

>>> from timeit import timeit
>>> timeit('"".join("strip my spaces".split())')
0.17734256500003198
>>> timeit('"strip my spaces".translate(ws_dict)', 'import string; ws_dict = {ord(ws):None for ws in string.whitespace}')
0.457635745999994
>>> timeit('re.sub(r"\s+", "", "strip my spaces")', 'import re')
1.017787621000025

>>> SETUP = 'import string, operator, functools, itertools; ws = set(string.whitespace)'
>>> timeit('"".join([c for c in "strip my spaces" if c not in ws])', SETUP)
0.6484303600000203
>>> timeit('"".join(c for c in "strip my spaces" if c not in ws)', SETUP)
0.950212219999969
>>> timeit('"".join(filter(lambda c: c not in ws, "strip my spaces"))', SETUP)
1.3164566040000523
>>> timeit('"".join(functools.reduce(lambda acc, c: acc if c in ws else acc+c, "strip my spaces"))', SETUP)
1.6947649049999995

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
8
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
27
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
28
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
29
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
30

Examples:

Input  : g e e k
Output : geek

Input  : Hello World
Output : HelloWorld

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
31
geek
8
geek
3
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
34
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
35

>>> "".join(filter(lambda c: c not in ws, "strip my spaces")) 'stripmyspaces' 8>>> "".join(filter(lambda c: c not in ws, "strip my spaces")) 'stripmyspaces' 9 >>> "".join(filter(lambda c: c not in ws, "strip my spaces")) 'stripmyspaces' 43sing replace() function

Python3

Rstrip () tạo ra một chuỗi mới bằng cách loại bỏ khoảng trắng kéo dài. Việc loại bỏ các không gian trắng từ bên của chuỗi bên phải làm cho nó đơn giản hơn để nhớ lại.

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
03
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
15
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
06
>>> from timeit import timeit
>>> timeit('"".join("strip my spaces".split())')
0.17734256500003198
>>> timeit('"strip my spaces".translate(ws_dict)', 'import string; ws_dict = {ord(ws):None for ws in string.whitespace}')
0.457635745999994
>>> timeit('re.sub(r"\s+", "", "strip my spaces")', 'import re')
1.017787621000025

>>> SETUP = 'import string, operator, functools, itertools; ws = set(string.whitespace)'
>>> timeit('"".join([c for c in "strip my spaces" if c not in ws])', SETUP)
0.6484303600000203
>>> timeit('"".join(c for c in "strip my spaces" if c not in ws)', SETUP)
0.950212219999969
>>> timeit('"".join(filter(lambda c: c not in ws, "strip my spaces"))', SETUP)
1.3164566040000523
>>> timeit('"".join(functools.reduce(lambda acc, c: acc if c in ws else acc+c, "strip my spaces"))', SETUP)
1.6947649049999995
9

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
3
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
5

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
6
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
7

Đầu ra

geek

Phương pháp 7: Xóa khoảng trống khỏi chuỗi bằng hàm rstrip ()Method 2: Remove spaces from a string using split() and join() 

Rstrip () tạo ra một chuỗi mới bằng cách loại bỏ khoảng trắng kéo dài. Việc loại bỏ các không gian trắng từ bên của chuỗi bên phải làm cho nó đơn giản hơn để nhớ lại.

Python3

Rstrip () tạo ra một chuỗi mới bằng cách loại bỏ khoảng trắng kéo dài. Việc loại bỏ các không gian trắng từ bên của chuỗi bên phải làm cho nó đơn giản hơn để nhớ lại.

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
03
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
15
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
06
>>> from timeit import timeit
>>> timeit('"".join("strip my spaces".split())')
0.17734256500003198
>>> timeit('"strip my spaces".translate(ws_dict)', 'import string; ws_dict = {ord(ws):None for ws in string.whitespace}')
0.457635745999994
>>> timeit('re.sub(r"\s+", "", "strip my spaces")', 'import re')
1.017787621000025

>>> SETUP = 'import string, operator, functools, itertools; ws = set(string.whitespace)'
>>> timeit('"".join([c for c in "strip my spaces" if c not in ws])', SETUP)
0.6484303600000203
>>> timeit('"".join(c for c in "strip my spaces" if c not in ws)', SETUP)
0.950212219999969
>>> timeit('"".join(filter(lambda c: c not in ws, "strip my spaces"))', SETUP)
1.3164566040000523
>>> timeit('"".join(functools.reduce(lambda acc, c: acc if c in ws else acc+c, "strip my spaces"))', SETUP)
1.6947649049999995
9

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
3
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
5

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
6
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
7

Đầu ra

geek

Phương pháp 7: Xóa khoảng trống khỏi chuỗi bằng hàm rstrip ()Method 3: Remove spaces from a string using Python regex 

Rstrip () tạo ra một chuỗi mới bằng cách loại bỏ khoảng trắng kéo dài. Việc loại bỏ các không gian trắng từ bên của chuỗi bên phải làm cho nó đơn giản hơn để nhớ lại.

Python3

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
03
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
15
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
06
>>> from timeit import timeit
>>> timeit('"".join("strip my spaces".split())')
0.17734256500003198
>>> timeit('"strip my spaces".translate(ws_dict)', 'import string; ws_dict = {ord(ws):None for ws in string.whitespace}')
0.457635745999994
>>> timeit('re.sub(r"\s+", "", "strip my spaces")', 'import re')
1.017787621000025

>>> SETUP = 'import string, operator, functools, itertools; ws = set(string.whitespace)'
>>> timeit('"".join([c for c in "strip my spaces" if c not in ws])', SETUP)
0.6484303600000203
>>> timeit('"".join(c for c in "strip my spaces" if c not in ws)', SETUP)
0.950212219999969
>>> timeit('"".join(filter(lambda c: c not in ws, "strip my spaces"))', SETUP)
1.3164566040000523
>>> timeit('"".join(functools.reduce(lambda acc, c: acc if c in ws else acc+c, "strip my spaces"))', SETUP)
1.6947649049999995
9

Rstrip () tạo ra một chuỗi mới bằng cách loại bỏ khoảng trắng kéo dài. Việc loại bỏ các không gian trắng từ bên của chuỗi bên phải làm cho nó đơn giản hơn để nhớ lại.

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
03
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
15
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
06
>>> from timeit import timeit
>>> timeit('"".join("strip my spaces".split())')
0.17734256500003198
>>> timeit('"strip my spaces".translate(ws_dict)', 'import string; ws_dict = {ord(ws):None for ws in string.whitespace}')
0.457635745999994
>>> timeit('re.sub(r"\s+", "", "strip my spaces")', 'import re')
1.017787621000025

>>> SETUP = 'import string, operator, functools, itertools; ws = set(string.whitespace)'
>>> timeit('"".join([c for c in "strip my spaces" if c not in ws])', SETUP)
0.6484303600000203
>>> timeit('"".join(c for c in "strip my spaces" if c not in ws)', SETUP)
0.950212219999969
>>> timeit('"".join(filter(lambda c: c not in ws, "strip my spaces"))', SETUP)
1.3164566040000523
>>> timeit('"".join(functools.reduce(lambda acc, c: acc if c in ws else acc+c, "strip my spaces"))', SETUP)
1.6947649049999995
9

Phương pháp 8: Sử dụng phương thức ISSPace ()

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
3
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
5

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
6
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
7

Đầu ra

geek

Phương pháp 7: Xóa khoảng trống khỏi chuỗi bằng hàm rstrip ()Method 4: Remove spaces from a string using translate() 

Rstrip () tạo ra một chuỗi mới bằng cách loại bỏ khoảng trắng kéo dài. Việc loại bỏ các không gian trắng từ bên của chuỗi bên phải làm cho nó đơn giản hơn để nhớ lại.

Python3

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
03
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
15
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
06
>>> from timeit import timeit
>>> timeit('"".join("strip my spaces".split())')
0.17734256500003198
>>> timeit('"strip my spaces".translate(ws_dict)', 'import string; ws_dict = {ord(ws):None for ws in string.whitespace}')
0.457635745999994
>>> timeit('re.sub(r"\s+", "", "strip my spaces")', 'import re')
1.017787621000025

>>> SETUP = 'import string, operator, functools, itertools; ws = set(string.whitespace)'
>>> timeit('"".join([c for c in "strip my spaces" if c not in ws])', SETUP)
0.6484303600000203
>>> timeit('"".join(c for c in "strip my spaces" if c not in ws)', SETUP)
0.950212219999969
>>> timeit('"".join(filter(lambda c: c not in ws, "strip my spaces"))', SETUP)
1.3164566040000523
>>> timeit('"".join(functools.reduce(lambda acc, c: acc if c in ws else acc+c, "strip my spaces"))', SETUP)
1.6947649049999995
9

Rstrip () tạo ra một chuỗi mới bằng cách loại bỏ khoảng trắng kéo dài. Việc loại bỏ các không gian trắng từ bên của chuỗi bên phải làm cho nó đơn giản hơn để nhớ lại.

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
03
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
15
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
06
>>> from timeit import timeit
>>> timeit('"".join("strip my spaces".split())')
0.17734256500003198
>>> timeit('"strip my spaces".translate(ws_dict)', 'import string; ws_dict = {ord(ws):None for ws in string.whitespace}')
0.457635745999994
>>> timeit('re.sub(r"\s+", "", "strip my spaces")', 'import re')
1.017787621000025

>>> SETUP = 'import string, operator, functools, itertools; ws = set(string.whitespace)'
>>> timeit('"".join([c for c in "strip my spaces" if c not in ws])', SETUP)
0.6484303600000203
>>> timeit('"".join(c for c in "strip my spaces" if c not in ws)', SETUP)
0.950212219999969
>>> timeit('"".join(filter(lambda c: c not in ws, "strip my spaces"))', SETUP)
1.3164566040000523
>>> timeit('"".join(functools.reduce(lambda acc, c: acc if c in ws else acc+c, "strip my spaces"))', SETUP)
1.6947649049999995
9

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
3
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
5

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
6
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
7

Đầu ra

geek

Phương pháp 7: Xóa khoảng trống khỏi chuỗi bằng hàm rstrip ()sing reduce function and conditional statement

Rstrip () tạo ra một chuỗi mới bằng cách loại bỏ khoảng trắng kéo dài. Việc loại bỏ các không gian trắng từ bên của chuỗi bên phải làm cho nó đơn giản hơn để nhớ lại.

Python3

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
03
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
15
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
06
>>> from timeit import timeit
>>> timeit('"".join("strip my spaces".split())')
0.17734256500003198
>>> timeit('"strip my spaces".translate(ws_dict)', 'import string; ws_dict = {ord(ws):None for ws in string.whitespace}')
0.457635745999994
>>> timeit('re.sub(r"\s+", "", "strip my spaces")', 'import re')
1.017787621000025

>>> SETUP = 'import string, operator, functools, itertools; ws = set(string.whitespace)'
>>> timeit('"".join([c for c in "strip my spaces" if c not in ws])', SETUP)
0.6484303600000203
>>> timeit('"".join(c for c in "strip my spaces" if c not in ws)', SETUP)
0.950212219999969
>>> timeit('"".join(filter(lambda c: c not in ws, "strip my spaces"))', SETUP)
1.3164566040000523
>>> timeit('"".join(functools.reduce(lambda acc, c: acc if c in ws else acc+c, "strip my spaces"))', SETUP)
1.6947649049999995
9

Rstrip () tạo ra một chuỗi mới bằng cách loại bỏ khoảng trắng kéo dài. Việc loại bỏ các không gian trắng từ bên của chuỗi bên phải làm cho nó đơn giản hơn để nhớ lại.

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
8
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
9
geek
7___

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
3
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
5

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
6
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
7

Output:

geek

Phương pháp 6: Xóa khoảng trống khỏi chuỗi bằng hàm lstrip ()Remove spaces from a string using lstrip() function

Lstrip () tạo ra một chuỗi mới bằng cách xóa khoảng trắng khỏi chuỗi bên trái bên trái hoặc khoảng trắng hàng đầu của nó.

Python3

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
3
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
02

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
03
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
05
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
06
>>> from timeit import timeit
>>> timeit('"".join("strip my spaces".split())')
0.17734256500003198
>>> timeit('"strip my spaces".translate(ws_dict)', 'import string; ws_dict = {ord(ws):None for ws in string.whitespace}')
0.457635745999994
>>> timeit('re.sub(r"\s+", "", "strip my spaces")', 'import re')
1.017787621000025

>>> SETUP = 'import string, operator, functools, itertools; ws = set(string.whitespace)'
>>> timeit('"".join([c for c in "strip my spaces" if c not in ws])', SETUP)
0.6484303600000203
>>> timeit('"".join(c for c in "strip my spaces" if c not in ws)', SETUP)
0.950212219999969
>>> timeit('"".join(filter(lambda c: c not in ws, "strip my spaces"))', SETUP)
1.3164566040000523
>>> timeit('"".join(functools.reduce(lambda acc, c: acc if c in ws else acc+c, "strip my spaces"))', SETUP)
1.6947649049999995
9

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
6
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
09

Đầu ra

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
1

Phương pháp 7: Xóa khoảng trống khỏi chuỗi bằng hàm rstrip ()Remove spaces from a string using rstrip() function

Rstrip () tạo ra một chuỗi mới bằng cách loại bỏ khoảng trắng kéo dài. Việc loại bỏ các không gian trắng từ bên của chuỗi bên phải làm cho nó đơn giản hơn để nhớ lại.

Python3

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
3
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
02

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
03
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
05
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
06
>>> from timeit import timeit
>>> timeit('"".join("strip my spaces".split())')
0.17734256500003198
>>> timeit('"strip my spaces".translate(ws_dict)', 'import string; ws_dict = {ord(ws):None for ws in string.whitespace}')
0.457635745999994
>>> timeit('re.sub(r"\s+", "", "strip my spaces")', 'import re')
1.017787621000025

>>> SETUP = 'import string, operator, functools, itertools; ws = set(string.whitespace)'
>>> timeit('"".join([c for c in "strip my spaces" if c not in ws])', SETUP)
0.6484303600000203
>>> timeit('"".join(c for c in "strip my spaces" if c not in ws)', SETUP)
0.950212219999969
>>> timeit('"".join(filter(lambda c: c not in ws, "strip my spaces"))', SETUP)
1.3164566040000523
>>> timeit('"".join(functools.reduce(lambda acc, c: acc if c in ws else acc+c, "strip my spaces"))', SETUP)
1.6947649049999995
9

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
6
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
09

Đầu ra

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
2

Phương pháp 7: Xóa khoảng trống khỏi chuỗi bằng hàm rstrip ()

Python3

Rstrip () tạo ra một chuỗi mới bằng cách loại bỏ khoảng trắng kéo dài. Việc loại bỏ các không gian trắng từ bên của chuỗi bên phải làm cho nó đơn giản hơn để nhớ lại.

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
8
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
23
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
25

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
03
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
15
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
06
>>> from timeit import timeit
>>> timeit('"".join("strip my spaces".split())')
0.17734256500003198
>>> timeit('"strip my spaces".translate(ws_dict)', 'import string; ws_dict = {ord(ws):None for ws in string.whitespace}')
0.457635745999994
>>> timeit('re.sub(r"\s+", "", "strip my spaces")', 'import re')
1.017787621000025

>>> SETUP = 'import string, operator, functools, itertools; ws = set(string.whitespace)'
>>> timeit('"".join([c for c in "strip my spaces" if c not in ws])', SETUP)
0.6484303600000203
>>> timeit('"".join(c for c in "strip my spaces" if c not in ws)', SETUP)
0.950212219999969
>>> timeit('"".join(filter(lambda c: c not in ws, "strip my spaces"))', SETUP)
1.3164566040000523
>>> timeit('"".join(functools.reduce(lambda acc, c: acc if c in ws else acc+c, "strip my spaces"))', SETUP)
1.6947649049999995
9

Phương pháp 8: Sử dụng phương thức ISSPace ()

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
36
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
23
geek
6
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
40

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
6
>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'
7

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
3
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
4
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
5

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
6
>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'
7