Previous Post :
Python #24-Reversing Of Given String
Next Post :
Coming Soon...
____________________________________
Python #24-Reversing Of Given String
Next Post :
Coming Soon...
Tips : To get each character from the word use "."
Source Code :
import re
result=re.findall(r".", "I AM BIG FAN OF SACHIN")
print result
Output:
['I', ' ', 'A', 'M', ' ', 'B', 'I', 'G', ' ', 'F', 'A', 'N', ' ', 'O', 'F', ' ', 'S', 'A', 'C', 'H', 'I', 'N']
Note Worthy : If you notice the output space is also present, in order to remove use "\w" instead of ".".
_________________________________________________
Tips : To get each character from the word use "\w" without space " " in output.
Source Code :
import re
result=re.findall(r"\w", "I AM A BIG FAN OF SACHIN")
print result
Output:
['I', 'A', 'M', 'A', 'B', 'I', 'G', 'F', 'A', 'N', 'O', 'F', 'S', 'A', 'C', 'H', 'I', 'N']
_________________________________________________
Tips : To get each word use "*" or "+"
Source Code :
import re
result=re.findall(r"\w*", "I AM A BIG FAN OF SACHIN")
print result
Output:
['I', '', 'AM', '', 'A', '', 'BIG', '', 'FAN', '', 'OF', '', 'SACHIN', '']
Note Worthy : Again, it is returning space as a word because "*" returns zero or more matches of pattern to its left. Now to remove spaces we will go with "+".
_________________________________________________
Source Code :
import re
result=re.findall(r"\w+", "I AM A BIG FAN OF SACHIN")
print result
Output:
['I', 'AM', 'A', 'BIG', 'FAN', 'OF', 'SACHIN']
_________________________________________________
NEED TO COMPILER TO CHECK CLICK ME
_________________________________________________
Tips: To get each word use "^"
Source Code :
import re
result=re.findall(r"^\w+", "I LOVE TO CODE")
print result
Output:
['I']
Note Worthy : If we will use "$" instead of "^", it will return the word from the end of the string. Let’s look at it.
_________________________________________________
Source Code :
import re
result=re.findall(r"\w+$", "I LOVE TO CODE")
print result
Output :
['CODE']
____________________________________
_________________________________________________
NEED TO COMPILER TO CHECK CLICK ME
_________________________________________________
Regex in Python :
- Python #21-Regular Expression In python
- Python #22-Regex match() and search() in python
- Python #23-Regex findall() and sub() in python
Thanks and regards,
Tech Bird
Comments