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