๐๐ป Handling texts
Handling texts using Python's built-in functions
๐ Notebooks
๐ Python built-in functions
๐ Length of a string
๐ข Number of characters
text = "Beauty always reserved in details, don't let the big picture steal your attention!"
len(text)
# 82
๐งพ Number of words
text = "Beauty always reserved in details, don't let the big picture steal your attention!"
words = text.split(' ')
len(words)
# 13
4๏ธโฃ Getting words have length greater than 4
text = "Beauty always reserved in details, don't let the big picture steal your attention!"
words = text.split(' ')
moreThan4 = [w for w in words if len(w) > 4]
# ['Beauty', 'always', 'reserved', 'details,', "don't", 'picture', 'steal', 'attention!']
๐ Words properties
๐ Getting capitalized words
text = "Beauty Always reserved in details, Don't let the big picture steal your attention!"
words = text.split(' ')
capitalized = [w for w in words if w.istitle()]
# ['Beauty', 'Always']
# "Don't" is not found ๐
๐ Getting words end with specific end
or specific start
.startswith()
text = "You can hide whatever you want to hide but your eyes will always expose you, eyes never lie."
words = text.split(' ')
endsWithEr = [w for w in words if w.endswith('er')]
# ['whatever', 'never']
๐ฅ Upper and lower
"ESMA".isupper() # True
"Esma".isupper() # False
"esma".isupper() # False
"esma".islower() # True
"ESMA".islower() # False
"Esma".islower() # False
๐คต Membership test
'm' in 'esma' # True
'es' in 'esma' # True
'ed' in 'esma' # False
๐ต๏ธโโ๏ธ Unique Words
๐ Case sensitive
text = "To be or not to be"
words = text.split(' ')
unique = set(words)
# {'be', 'To', 'not', 'or', 'to'}
โ๏ธ ๐ Ignore case
text = "To be or not to be"
words = text.split(' ')
unique = set(w.lower() for w in words)
# {'not', 'or', 'be', 'to'}
๐ฎโโ๏ธ Checking Ops
Is Digit?
'17'.isdigit() # True
'17.7'.isdigit() # False
Is Alphabetic?
'esma'.isalpha() # True
'esma17'.isalpha() # False
Is alphabetic or number?
'17esma'.isalnum() # True
'17esma;'.isalnum() # False
๐ค String Ops
"Esma".lower() # esma
"Esma".upper() # ESMA
"EsmA".title() # Esma
๐งต Split & Join
Split due to specific character
text = "Beauty,Always,reserved,in,details,Don't,let,the,big,picture,steal,your,attention!"
words = text.split(',')
# ['Beauty', 'Always', 'reserved', 'in', 'details', "Don't", 'let', 'the', 'big', 'picture', 'steal', 'your', 'attention!']
Join by specific character
text = "Beauty,Always,reserved,in,details,Don't,let,the,big,picture,steal,your,attention!"
words = text.split(',')
joined = " ".join(words)
# Beauty Always reserved in details Don't let the big picture steal your attention!
Last updated
Was this helpful?