Python Forum

Full Version: <class 'str'> ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Why is that '\d' in a regular expression finds digits but then if those digits are stored in a variable, print(type(variable)) returns <class 'str'>?

import re

string = '''
10.00 22.34 31.23
'''

test_re = re.compile(r'\d')
test = test_re.findall(string)
print(test)

for i in test:
 print(type(i))
Output:
['1', '0', '0', '0', '2', '2', '3', '4', '3', '1', '2', '3'] <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'>
regular expressions are all about strings. It's a char that is a digit, it is not expected to cast the result to numeric type. It's also evident from the list that you print - it's a list of str, not int
Because digits are strings. Did you think it was converting the string to an int?