开发者

Python remove spaces and append

I am currently writing a growl notification plugin for emesene messenger on OS X. It is nearly working except to when it comes to displaying a message snippet.

The message is passed to growlnotify as a variable, however growl does not accept spaces in the displayed message.

So what i开发者_运维问答 need help with is a script to remove the spaces between multiple words and replace it with a \ then a space.

e.g. Original: This is a message What is needed: This\ is\ a\ message

I have looked around at similar answers but i could not work out how to append the slash.


Instead of trying to do this with os.system(), use subprocess instead, passing the program and arguments as a list.


Just use the replace method of the string class:

message_string = "This is a message"
print message_string.replace(" ", "\ ")

returns:

$ python test.py
This\ is\ a\ message

See Python string.replace documentation.


message = "This is a message"
print message.replace( " ", "\ " )


You can do this using the replace method of strings.


Everyone's using replace, so here's the other solution:

print '\ '.join(message.split(' '))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜