Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 14d3113

Browse files
author
SamLaFell
committed
Updates to files
1 parent 49bcf06 commit 14d3113

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Pages 147 - 153 in ATBS textbook
2+
# Pattern Matching and Regular Expressions
3+
4+
## Ex... Phone Number
5+
# 919-675-9338 <- A phone number
6+
# 4,150,000 <- Not a phone number
7+
8+
9+
##Non-RegEx Example
10+
# def isPhoneNumber(text):
11+
# if len(text) != 12:
12+
# return False #not phone number
13+
# for i in range(0,3):
14+
# if not text[i].isdecimal():
15+
# return False #no area code
16+
# if text[3] != '-':
17+
# return False #missing dash
18+
# for i in range(4,7):
19+
# if not text[i].isdecimal():
20+
# return False #no first 3 digits
21+
# if text[7] != '-':
22+
# return False #missing second dash
23+
# for i in range(8,12):
24+
# if not text[i].isdecimal():
25+
# return False #missing last 4 digits
26+
# return True
27+
#
28+
# print(isPhoneNumber('919-675-9338'))
29+
#
30+
#
31+
# message = 'Call me at 415-555-1011 tomorrow, or at 919-675-9338 over the weekend for my personal line'
32+
# foundNumber = False
33+
# for i in range(len(message)):
34+
# chunk = message[i:i+12]
35+
# if isPhoneNumber(chunk):
36+
# print('Phone number found:', chunk)
37+
# foundNumber = True
38+
# if not foundNumber:
39+
# print('Could not find any phone numbers.')
40+
41+
42+
43+
44+
# So now, let's decrease the lines of code we need for that
45+
import re
46+
message = 'Call me at 415-555-1011 tomorrow, or at 919-675-9338 over the weekend.'
47+
48+
# phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
49+
# mo = phoneNumRegex.search(message)
50+
# print(mo.group())
51+
52+
phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
53+
print(phoneNumRegex.findall(message))
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# spam = 'Hello World!'
2+
# print(spam.islower())
3+
#
4+
# spam='hello world!'
5+
# print(spam.islower())
6+
#
7+
# spam = 'HELLO WORLD!'
8+
# print(spam.isupper())
9+
#
10+
# spam = ''
11+
# print(spam.isupper())
12+
# print(spam.islower())
13+
14+
# '12345'.islower()
15+
16+
# print('Hello'.upper().isupper())
17+
18+
'''Other operators:
19+
isalpha() -- Letters
20+
isalnum() -- Letters and numbers
21+
isdecimal() -- Numbers
22+
isspace() -- whitespace
23+
istitle() -- titlecase
24+
'''
25+
26+
# print('hello'.isalpha())
27+
# print('hello123'.isalpha())
28+
# print('hello123'.isalnum())
29+
# print('123'.isdecimal())
30+
# print(' '.isspace())
31+
# print('hello world!'.isspace())
32+
# print('hello world!'[5].isspace())
33+
# print('This Is Title Case.'.istitle())
34+
35+
#Starts with and ends with...
36+
# print('Hello World!'.startswith('Hello'))
37+
# print('Hello World!'.startswith('H'))
38+
# print('Hello World!'.startswith('ello'))
39+
# print('Hello World!'.endswith('World!'))
40+
41+
42+
43+
# join
44+
print(','.join(['cats', 'rats','bats']))
45+
print(''.join(['cats', 'rats','bats']))
46+
print('\n\n'.join(['cats', 'rats','bats']))
47+
48+
# .split
49+
print('My name is Simon'.split())
50+
51+
print('My name is Simon'.split('m'))
52+
53+
#ljust() and rjust() and center()
54+
print('Hello'.rjust(10))
55+
print('Hello'.ljust(20))
56+
print('Hello'.rjust(10, '*'))
57+
print('Hello'.ljust(25, '-'))
58+
print('Hello'.center(20))
59+
print('Hello'.center(20, '='))
60+
61+
62+
#strip, rstrip, lstrip
63+
spam = 'Hello'.rjust(10)
64+
spam.strip()
65+
print(spam)
66+
spam = spam.strip()
67+
print(spam)
68+
69+
print(' z '.strip())
70+
print(' z '.lstrip())
71+
print(' z '.rstrip())
72+
73+
74+
print('SpamSpamBaconSpamEggsSpamSpam'.strip('ampS'))
75+
76+
77+
78+
# replace
79+
spam = 'Hello there!'
80+
print(spam.replace('e', 'XYZ'))
81+
82+
83+
# The pyperclip module -- Copy and paste from the clipboard
84+
import pyperclip
85+
pyperclip.copy('Hello!!!!!!!!')
86+
print(pyperclip.paste())
87+
88+
89+
90+
91+
# String Formatting
92+
93+
print('hello ' + 'world!')
94+
95+
name = 'Alice'
96+
place = 'Main Street'
97+
time = '6 pm'
98+
food = 'turnips'
99+
100+
print('Hello ' + name + ', you are invited to a party at ' \
101+
+ place + ' at ' + time + '. Please bring ' + food + '.')
102+
103+
print('Hello %s, you are invited to a party at %s at\
104+
%s. Please bring %s.' % (name, place, time, food))
105+
106+
107+
print('-'.join('There can be only one.'.split()))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /