Search code examples
pythonxchat2

Pad outgoing messages


I can't quite figure out how to pad a message that I send. Basically.. I want to pad the message to the max 512 chars as defined in the RFC.

I understand that a message being sent by the user will contain user!user@hostname privmsg #chan (or other_user): text text text \r\n.

Thanks in advance.


Solution

  • To pad a string you can use the functions ljust, center or rjust, respectively:

    print "Hello, " + "world".ljust(10) + "!"
    print "Hello, " + "world".center(10) + "!"
    print "Hello, " + "world".rjust(10) + "!"
    

    Output (Try it):

    Hello, world     !
    Hello,   world   !
    Hello,      world!
    

    All three functions have an optional second argument fillchar which lets you specify the character that is used to fill up the additional space.