string character count

Iain King iainking at gmail.com
Wed Jul 1 05:52:24 EDT 2009


On Jun 30, 6:27 pm, noydb <jenn.du... at gmail.com> wrote:
> If I have a string for a file name such that I want to find the number
> of characters to the left of the dot, how can that be done?
>
> I did it this way:
> x = "text12345.txt"
> dot = x.find('.')
> print dot
>
> Was curious to see what method others would use - helps me learn.  I
> guess I was most curious to see if it could be done in one line.
>
> And, how would a char count be done with no dot -- like if the string
> were "textstringwithoutdot" or "no dot in text string"?

import os
root, ext = os.path.splitext(x)
print len(root)

or in one line (assuming you've imported os):
print len(os.path.splitext(x)[0])


Iain



More information about the Python-list mailing list