Possible Duplicate:
Is there a way to substring a string in Python?
I have a string in the form 'AAAH8192375948'. How do I keep the first 5 characters of this string, and strip all the rest? Is it in the form l.strip with a negative integer? Thanks.
Possible Duplicate:
Is there a way to substring a string in Python?
I have a string in the form 'AAAH8192375948'. How do I keep the first 5 characters of this string, and strip all the rest? Is it in the form l.strip with a negative integer? Thanks.
A string in Python is a sequence type, like a list or a tuple. Simply grab the first 5 characters:
 some_var = 'AAAH8192375948'[:5]
 print some_var # AAAH8
The slice notation is [start:end:increment] -- numbers are optional if you want to use the defaults (start defaults to 0, end to len(my_sequence) and increment to 1).  So:
 sequence = [1,2,3,4,5,6,7,8,9,10] # range(1,11)
 sequence[0:5:1] == sequence[0:5] == sequence[:5] 
 # [1, 2, 3, 4, 5]
 sequence[1:len(sequence):1] == sequence[1:len(sequence)] == sequence[1:]
 # [2, 3, 4, 5, 6, 7, 8, 9, 10]
 sequence[0:len(sequence):2] == sequence[:len(sequence):2] == sequence[::2]
 # [1, 3, 5, 7, 9]
strip removes a character or set of characters from the beginning and end of the string - entering a negative number simply means that you are attempting to remove the string representation of that negative number from the string.
 
    
    Have you heard about slicing ?
>>> # slice the first 5 characters
>>> first_five = string[:5]
>>>
>>> # strip the rest
>>> stripped = string[5:].strip()
>>>
>>> # in short:
>>> first_five_and_stripped = string[:5], string[5:].strip()
>>>
>>> first_five_and_stripped
('AAAH8', '192375948')
 
    
    I'm assuming you don't just mean "get rid of everything but the first 5 chars", but rather "keep the first 5 chars and run strip() on the rest".
>>> x = 'AAH8192375948'
>>> x[:5]
'AAH81'
>>> x[:5] + x[5:].strip()
'AAH8192375948'
