Reading after a symbol..

Chris Rebert clp2 at rebertia.com
Tue Oct 12 23:25:41 CEST 2010


On Tue, Oct 12, 2010 at 1:48 PM, Pratik Khemka <pratikkhemka at hotmail.com> wrote:
> Say :  line = abcdabcd#12 adssda
>
> index = line.find('#')
> num = line[index:index+2]
>
> num will now be 12.

No, num will be "#1". You wanted:
num = line[index+1:index+3]

> Likewise I want to read the number after the '#' and store it in num. The
> problem is that the number can be a 1/2/3/4 digit number. So is there a way
> in which I can define num so that it contains the number after '#'
> irrespective of how many digits the number is. Because the problem is that
> the above code will not work for scenarios when the number is  not 2
> digits..

Sure. From your example, the number appears to be delimited by a # and
a space. So:
after_pound = line[line.index('#')+1:]
num = after_pound[:after_pound.index(' ')]

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list