Trim the center of a string

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def trim_center_of_string(string,max_size):
    ''' 
    String: hellogoodbye 
    max_size: 8
    return hellbyes
    '''
    if len(string) >= max_size:
        # lets trim the center our long string
        length = len(string)
        excess = len(string) - max_size 
        left_imit = (length/2)-(excess/2)  # hell|os&goodbyes
        right_limit = left_imit+excess     # hellos&good|byes
        left_part = string[:left_imit]     # hell|
        right_part = string[right_limit:-1]# |byes
        string = left_part+right_part      # hellbyes
    return string

More like this

  1. datetime.time/datetime.datetime to Unix Epoch (with microsecond support) by sleepycal 3 years ago
  2. models.py with django_dag models for parts hierarchy by j_syk 1 year, 9 months ago
  3. Switch/case tags. by jacobian 5 years, 10 months ago
  4. Arbitrary length formset by Rupe 3 years, 8 months ago
  5. Slice filter by sneeu 6 years, 2 months ago

Comments

(Forgotten your password?)