python中去掉空格的方法有以下幾種
1.使用lstrip()函數(shù)去掉左邊空格
string = " * it is blank space test * "print (string.lstrip())
輸出結(jié)果為:
* it is blank space test *
2.使用rstrip()函數(shù)去掉右邊空格
string = " * it is blank space test * "print (string.rstrip())
輸出結(jié)果為:
* it is blank space test *
3.使用strip()函數(shù)去掉左右兩邊空格
string = " * it is blank space test * "print (string.strip())
輸出結(jié)果為:
* it is blank space test *
4.使用replace()函數(shù)去掉所有空格
string = " * it is blank space test * "str_new = string.replace(" ", "")print str_new
輸出結(jié)果為:
*itisblankspacetest