Convert String to Int and Arithmetic

tereglow tereglow at yahoo.com
Tue Jun 12 11:14:53 EDT 2007


On Jun 12, 10:46 am, Kay Schluehr <kay.schlu... at gmx.net> wrote:
> On 12 Jun., 16:32, tereglow <tereg... at yahoo.com> wrote:
>
> > Hello,
>
> > I am a complete newbie to Python and am accustomed to coding in PHP/
> > Perl/Shell.  I am trying to do the following:
>
> > I have a string:
>
> > cpuSpeed = 'Speed: 1000000000'
>
> > What I would like to do is extract the '1000000000' from the string,
> > and divide that by 1000 twice to get the speed of a processor in MHz.
>
> > My understanding is that I need to 'import re' and then use re.split
> > to get the first part done.
>
> It shall suffice to use string methods in this case:
>
> >>> int(cpuSpeed.split(":")[1].strip())
>
> 1000000000
>
> Finally you might trim the numerical value by division just as you
> wish.

Thanks guys.  The biggest problem I'm having with Python is that it
makes things too easy to do!




More information about the Python-list mailing list