Cheuk Ting Ho
Developer advocate / Data Scientist - support open-source and building the community.
Online Absolute Beginner Python Tutorials
Every Sunday 2pm (UK time/ BST)
by Cheuk Ting Ho
Get this slide deck: https://slides.com/cheukting_ho/python-strings-regex
Python objects - int, float, str, list, dict, bool
Control flows - if-else, for loop, while loop
Functions, modeuls, classes and decorators
pytest
Rrcap: "adding" two strings "+", f-strings
greeting = "Hello" + " " + "World"
more_greeting = f"Hey {greeting}! Do you want some tractor jokes?"
You can also use "join"
hello_list = ["Hello", "World"]
" ".join(hello_list)
How about breaking them?
greeting.split()
"what is www mean?".split('w')
"Happy!!!Sunday!".split("!!!")
"There_are/lots#of_special/characters".split("_/#")
Can I spint with multiple option? (Spoilor: Regex)
Other manipulations
"Hello World".lower()
"Hello World".upper()
"hello world".capitalize()
"!!!!!Yay it's Sunday!!".replace('!',' ')
" Hello ".strip()
"!!!!!Yay it's Sunday!!".strip('!')
" Yay it's Sunday !!! ".strip(" !")
What is Regex?
A regular expression (shortened as regex or regexp; also referred to as rational expression) is a sequence of characters that define a search pattern. - wikipedia
Whole Numbers – /^\d+$/
Common email Ids – /^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})*$/
Time Format HH:MM:SS 24-hour
/(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)/
Cheatsheet - https://www.rexegg.com/regex-quickstart.html
Tester - https://regex101.com/
Common Regex - https://digitalfortress.tech/tricks/top-15-commonly-used-regex/
How to use regex in Python?
import re
that_sentance = "There_are/lots#of_special/characters"
re.split("[_/#]",that_sentance)
Replace a pattern
import re
that_sentance = "There_are/lots#of_special/characters"
re.sub("[_/#]"," ",that_sentance)
Searching for a pattern
import re
that_sentance = "There_are/lots#of_special/characters"
re.search("[_/#]",that_sentance)
re.search("re",that_sentance)
>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m.group(0) # The entire match
'Isaac Newton'
>>> m.group(1) # The first parenthesized subgroup.
'Isaac'
>>> m.group(2) # The second parenthesized subgroup.
'Newton'
You can also check the "span()"
Simple E-comerce similator ( e-shop.py)
Can you validate the user's email address?
https://github.com/Cheukting/python02hero/tree/master/2020-05-31-python-strings-regex
There is no homework :-)
Sunday 2pm (UK time/ BST)
There are also Mid Meet Py every Wednesday 1pm
Testing month in June
By Cheuk Ting Ho
Developer advocate / Data Scientist - support open-source and building the community.