I want to strip all characters after a third character, say - for instance.
I found this code online and it works but I'm having trouble learning how it works and wanted to ask so I can understand it fully.
 def indexList(s, item, i=0):
    """
    Return an index list of all occurrances of 'item' in string/list 's'.
    Optional start search position 'i'
    """
    i_list = []
    while True:
        try:
            i = s.index(item, i)
            i_list.append(i)
            i += 1
        except:
            break
    return i_list
def strip_chrs(s, subs):
    for i in range(indexList(s, subs)[-1], len(s)):
        if s[i+1].isalpha():
            return data[:i+1]
data = '115Z2113-3-777-55789ABC7777'
print strip_chrs(data, '-')
Here's my questions on the while True: line what's true? Also on the except: Except what? and why does is a break coded there?
Thanks in advance!
Python Remove Character from String using replace() We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string.
a. split("-", 2) will split the string upto the second occurrence of - . a. split("-", 2)[:2] will give the first 2 elements in the list.
You can find the nth occurrence of a substring in a string by splitting at the substring with max n+1 splits. If the resulting list has a size greater than n+1, it means that the substring occurs more than n times.
Here is a way:
def trunc_at(s, d, n=3):
    "Returns s truncated at the n'th (3rd by default) occurrence of the delimiter, d."
    return d.join(s.split(d, n)[:n])
print trunc_at("115Z2113-3-777-55789ABC7777", "-")
How it works:
s is split into a list at each occurrence of the delimiter d using s.split(d). We use the second argument to split to indicate the maximum number of splits to do (since there's no reason to keep splitting after the first n times). The result is a list, such as ["115Z2113", "3", "777", "55789ABC7777"]
n items of the list is taken using [:n]. The result is another list, such as ["115Z2113", "3", "777"]
d between each item of the list,using d.join(...), resulting in, for example, "115Z2113-3-777"
In a one-liner way :
data = '115Z2113-3-777-55789ABC7777'
strip_character = "-"
>>> strip_character.join(data.split(strip_character)[:3])
'115Z2113-3-777'
                        On the "while True", True is simply the constant value True. So while True is loop-forever or until broken.
the except is using the exception that happens when s.index finds no more string after i as a way to break the loop. This is a Bad Thing.
try something like this (pseudocode):
while you still have string left:
   get index of next '-'
   if found, add 1 to count
   if count == 3:
      return s[index+1:]
the s[index+1:] returns the substring from the character following index, to the end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With