2017-07-08 70 views
-7

我有以下字符串刪除空格在每一個地方的字符串蟒蛇

a = " Get a Pen" 

代碼:

if a.lower().strip() == "get a pen" 
    print "removed white spaces" 

需要幫助實現刪除所有空格字符串

if a.lower.().strip() =="getapen" # which is the easiest way remove all spaces 
     print "remove all spaces in the string" 
+3

「strip.replace('','')'或'''.join(c for c in a c!='')' –

+0

strip只能刪除尾部和前導空格 –

+0

@ChristianDean哪一個' replace()'或'join()'是否更優化?我知道'join()'在線性時間內運行,但'replace()'怎麼辦? –

回答

-1

如@ChristianDean指出,做

if a.lower().replace(' ', '') == 'getapen': 
    print "remove all spaces in the string"