Reputation: 93813
I have a string like
"xdtwkeltjwlkejt7wthwk89lk"
how can I get the index of the first digit in the string?
Upvotes: 61
Views: 138563
Reputation: 11
instr = 'nkfnkjbvhbef0njhb h2konoon8ll'
numidx = next((i for i, s in enumerate(instr) if s.isdigit()), None)
print(numidx)
Output:
12
numidx
will be the index of the first occurrence of a digit in instr
. If there are no digits in instr
, numidx
will be None
.
I didn't see this solution here, and thought it should be.
Upvotes: 0
Reputation:
In Python 3.8+ you can use re.search
to look for the first \d
(for digit) character class like this:
import re
my_string = "xdtwkeltjwlkejt7wthwk89lk"
if first_digit := re.search(r"\d", my_string):
print(first_digit.start())
Upvotes: 2
Reputation: 1
import re
result = " Total files:................... 90"
match = re.match(r".*[^\d](\d+)$", result)
if match:
print(match.group(1))
will output
90
Upvotes: -1
Reputation: 1539
import re
first_digit = re.search('\d', 'xdtwkeltjwlkejt7wthwk89lk')
if first_digit:
print(first_digit.start())
Upvotes: 11
Reputation: 36504
Use re.search()
:
>>> import re
>>> s1 = "thishasadigit4here"
>>> m = re.search(r"\d", s1)
>>> if m:
... print("Digit found at position", m.start())
... else:
... print("No digit in that string")
...
Digit found at position 13
Upvotes: 91
Reputation: 1042
To get all indexes do:
idxs = [i for i in range(0, len(string)) if string[i].isdigit()]
Then to get the first index do:
if len(idxs):
print(idxs[0])
else:
print('No digits exist')
Upvotes: 7
Reputation: 88737
Here is a better and more flexible way, regex is overkill here.
s = 'xdtwkeltjwlkejt7wthwk89lk'
for i, c in enumerate(s):
if c.isdigit():
print(i)
break
output:
15
To get all digits and their positions, a simple expression will do
>>> [(i, c) for i, c in enumerate('xdtwkeltjwlkejt7wthwk89lk') if c.isdigit()]
[(15, '7'), (21, '8'), (22, '9')]
Or you can create a dict of digit and its last position
>>> {c: i for i, c in enumerate('xdtwkeltjwlkejt7wthwk89lk') if c.isdigit()}
{'9': 22, '8': 21, '7': 15}
Upvotes: 40
Reputation: 1933
def first_digit_index(iterable):
try:
return next(i for i, d in enumerate(iterable) if d.isdigit())
except StopIteration:
return -1
This does not use regex and will stop iterating as soon as the first digit is found.
Upvotes: 0
Reputation: 6288
Thought I'd toss my method on the pile. I'll do just about anything to avoid regex.
sequence = 'xdtwkeltjwlkejt7wthwk89lk'
i = [x.isdigit() for x in sequence].index(True)
To explain what's going on here:
[x.isdigit() for x in sequence]
is going to translate the string into an array of booleans representing whether each character is a digit or not[...].index(True)
returns the first index value that True
is found in.Upvotes: 21
Reputation: 184151
Here is another regex-less way, more in a functional style. This one finds the position of the first occurrence of each digit that exists in the string, then chooses the lowest. A regex is probably going to be more efficient, especially for longer strings (this makes at least 10 full passes through the string and up to 20).
haystack = "xdtwkeltjwlkejt7wthwk89lk"
digits = "012345689"
found = [haystack.index(dig) for dig in digits if dig in haystack]
firstdig = min(found) if found else None
Upvotes: 2
Reputation: 2527
you can use regular expression
import re
y = "xdtwkeltjwlkejt7wthwk89lk"
s = re.search("\d",y).start()
Upvotes: 0
Reputation: 138347
As the other solutions say, to find the index of the first digit in the string we can use regular expressions:
>>> s = 'xdtwkeltjwlkejt7wthwk89lk'
>>> match = re.search(r'\d', s)
>>> print match.start() if match else 'No digits found'
15
>>> s[15] # To show correctness
'7'
While simple, a regular expression match is going to be overkill for super-long strings. A more efficient way is to iterate through the string like this:
>>> for i, c in enumerate(s):
... if c.isdigit():
... print i
... break
...
15
In case we wanted to extend the question to finding the first integer (not digit) and what it was:
>>> s = 'xdtwkeltjwlkejt711wthwk89lk'
>>> for i, c in enumerate(s):
... if c.isdigit():
... start = i
... while i < len(s) and s[i].isdigit():
... i += 1
... print 'Integer %d found at position %d' % (int(s[start:i]), start)
... break
...
Integer 711 found at position 15
Upvotes: 3
Reputation: 77251
Seems like a good job for a parser:
>>> from simpleparse.parser import Parser
>>> s = 'xdtwkeltjwlkejt7wthwk89lk'
>>> grammar = """
... integer := [0-9]+
... <alpha> := -integer+
... all := (integer/alpha)+
... """
>>> parser = Parser(grammar, 'all')
>>> parser.parse(s)
(1, [('integer', 15, 16, None), ('integer', 21, 23, None)], 25)
>>> [ int(s[x[1]:x[2]]) for x in parser.parse(s)[1] ]
[7, 89]
Upvotes: 10
Reputation: 1809
I'm sure there are multiple solutions, but using regular expressions you can do this:
>>> import re
>>> match = re.search("\d", "xdtwkeltjwlkejt7wthwk89lk")
>>> match.start(0)
15
Upvotes: 2