Here goes my trivial Python code:
from re import match
str = "Hello"
print str[::-1] # elloH
obj = match(r'(\w)(\w)(\w)(\w)(\w)', str)
for i in range(len(str)-1, -1, -1): # or reversed(range(len(str)))
print obj.groups()[i], # o l l e H
I have two queries in this code:
- What is the logic behind the range option in the list
str[::-1]
? Becausestr[len(str):-1:-1]
gives empty output andstr[len(str):0:-1]
gives output ``olle` and so on. - How to make this regular expression
r'(\w)(\w)(\w)(\w)(\w)'
so compact? (i.e) removing the repeating or recursive redundant(\w)
, something liker'(\w)+'
?