1.去掉左边空格
string = " * it is blank space test * "
print (string.lstrip())
result:
* it is blank space test *
2.去掉右边空格
string = " * it is blank space test * "
print (string.rstrip())
result:
* it is blank space test *
3.去掉左右两边空格
string = " * it is blank space test * "
print (string.strip())
result:
* it is blank space test *
4.去掉所有空格
有两种方式
string = " * it is blank space test * "
str_new = string.replace(" ", "")
print str_new
result:
*itisblankspacetest*
eg2:正则匹配把空格替换成空
import re
string = " * it is blank space test * "
str_new = re.sub(r"\s+", "", string)
print str_new
result:
*itisblankspacetest*
关于python去掉空格常用方式有哪些,环球青藤小编就和大家分享到这里了,学习是永无止境的,学习一项技能更是受益终身,所以,只要肯努力学,什么时候开始都不晚。如果您还想继续了解关于python编程的学习方法及素材等内容,可以点击本站其他文章学习。
使用PIL库中的ImageDraw.Draw()方法可以实现题述情况,具体步骤如下:1. 创建一个Image对象,并使用ImageDraw.Draw()方法创建一个画布。
2. 使用ImageDraw.text()方法将文字写入画布中。
3. 设置文字的宽度,并使用splitlines()方法将文字拆分成多行。
4. 使用for循环遍历每一行文字,并将其写入到画布中。
5. 保存图片即可。
将#替换为空格,代码如下:from ctypes import *
user32 = windll.user32
kernel32 = windll.kernel32
def set_clipboard(data):
####user32.OpenClipboard(c_int(0))
####user32.EmptyClipboard()
####alloc = kernel32.GlobalAlloc(0x2000, len(bytes(data, encoding='utf_8'))+1)
####lock = kernel32.GlobalLock(alloc)
####cdll.msvcrt.strcpy(c_char_p(lock),bytes(data, encoding='utf_8'))
####kernel32.GlobalUnlock(alloc)
####user32.SetClipboardData(c_int(1),alloc)
####user32.CloseClipboard()
set_clipboard("A\t1\t2\t3\nB\t1\t2\t4")