s = 'w w'
s.strip() # 为什么去除不了中间的空格
s = 'w w'
s.replace('\s',';') # 为什么替换不了,\s 表示空格没错吧。。
In fact, the final requirement is this:
means that (multiple) spaces, (multiple) line feeds, (multiple) carriage returns and other whitespace characters will appear in the middle and on both sides of the string. I want to convert these whitespace characters. into ";", is there any good way
Strip removes the spaces at the left and right ends, but the spaces in the middle cannot be removed.
replace cannot use regular expressions as parameters, you must use the
re
module.replace is used to replace one string with another string. For it, s is an ordinary string and has no other meaning.
For your needs, I think you can use str.maketrans and translate functions to achieve it.
My running environment is python3.5, the example is as follows:
s = 'w w'
transmap=str.maketrans({' ':';','r':';','n':';' })
print( s.translate(transmap) )
Run result:
w;w
Actually
";".join(s.split())
is fine