user15697
user15697

Reputation: 147

How do I add character padding to a string?

Okay this is very hard to explain, but i want to add padding to a sentence so that the characters in that sentence are a multiple of n.

however this number '4' has to change to 3 and 5 so it has to work then as well..

anyone know what im talking about?? and how to do it?

Upvotes: 0

Views: 2766

Answers (3)

user850498
user850498

Reputation: 727

def pad(yourString,blockLength):
    return yourString + ("X" * (blockLength - (len(yourString) % blockLength)))

Is your padding function. for end padding. If you need center padding use:

def centerPad(yourString,blockLength):
    return ("X" * ((blockLength - (len(yourString) % blockLength))/2)) + yourString + ("X" * ((blockLength - (len(yourString) % blockLength))/2))

you need to take a harder look at the rest of your code if you want to implement a block cypher.

Upvotes: 0

jamylak
jamylak

Reputation: 133604

>>> import math
>>> def encrypt(string, length):
        inverse_string = string.replace(' ','X')[::-1]

        center_width = int(math.ceil(len(inverse_string)/float(length)) * length) # Calculate nearest multiple of length rounded up

        inverse_string = inverse_string.center(center_width,'X')

        create_blocks = ' '.join(inverse_string[i:i+length] for i in xrange(0,len(inverse_string),length))
        return create_blocks

>>> encrypt('THE PRICE OF FREEDOM IS ETERNAL VIGILENCE', 4)
'XECN ELIG IVXL ANRE TEXS IXMO DEER FXFO XECI RPXE HTXX'

Upvotes: 0

Abhijit
Abhijit

Reputation: 63757

I hope the self commented code below would help you grasp the concept. You just have to do some maths to get the pad characters at either end

Some concept

  1. Extra Characters required Padding = len(string) % block_length
  2. Total_Pad_Characters = block_length - len(string) % block_length
  3. Pad Character's at front = Total_Pad_Characters/2
  4. Pad Character's at end = Total_Pad_Characters - Total_Pad_Characters/2

So here is the code

>>> def encrypt(st,length):
    #Reversed the String and replace all Spaces with 'X'
    st = st[::-1].replace(' ','X')
    #Find no of characters to be padded.
    padlength = (length - len(st)%length) % length
    #Pad the Characters at either end
    st = 'X'*(padlength/2)+st+'X'*(padlength-padlength/2)
    #Split it with size length and then join with a single space
    return ' '.join(st[i:i+length] for i in xrange(0,len(st),length))

>>> encrypt('THE PRICE OF FREEDOM IS ETERNAL VIGILENCE', 4) #Your Example
'XECN ELIG IVXL ANRE TEXS IXMO DEER FXFO XECI RPXE HTXX'
>>> encrypt('THE PRICE', 5) # One Extra Character at end for Odd Numbers
'ECIRP XEHTX'
>>> encrypt('THE PRIC', 5) # 1 Pad Characters at either end
'XCIRP XEHTX'
>>> encrypt('THE PRI', 5) # 1 Pad Characters at either end and one Extra for being Odd
'XIRPX EHTXX'
>>> encrypt('THE PR', 5) # 2 Pad Characters at either end
'XXRPX EHTXX'
>>> encrypt('THE P', 5) # No Pad characters required
'PXEHT'
>>> encrypt('THE PRICE OF FREEDOM IS ETERNAL VIGILENCE', 5) #Ashwini's Example
'XXECN ELIGI VXLAN RETEX SIXMO DEERF XFOXE CIRPX EHTXX'
>>>

Upvotes: 2

Related Questions