python中用什么函数去掉空格

978
2021/1/20 18:30:37
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

python中用什么函数去掉空格

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


辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: python元素定位的方法有哪些