# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
spam ="My name is Rahul"

print(spam)

# +
spam = "My name i\\s mother"

print(spam)
# -

# #### Raw strings completely ignores all escape characters and prints any backlash that appears in the stirng

print(r'That is Carol\'s cat.')

# #### Use  r before the beginning quotation mark of a string to make it a raw string.

# !python

'hello'.isalpha()

'hello123'.isalpha()


'hello'.isalnum()

# +
while True:
    print('Enter your age:')
    age = input()
    if age.isdecimal():
        break
    print('Please enter a number for your age.')
    
while True:
    print('Select a new password(letters and numbers only):')
    password = input()
    if password.isalnum():
        break
    print('Passwords can only have letters and numbers.')    
# -

'Hello world!'.startswith('Hello')

'Hello world!'.endswith('world!')


'abc123'.startswith('abcdef')

', '.join(['cats','rats','bats'])

' '.join(['My','name','is','Simon'])

'ABC'.join(['My','name','is','Simon'])

"My name is Madhav".split()

'MyABCnameABCisABCSimon'.split('ABC')

'My name is Simon'.split('m')

'Hello'.center(20, '=')

'Hello'.rjust(20, '*')

'Hello'.ljust(20, '-')

# ### Password Locker

# +
# #! python3
# -

PASSWORDS = {'email':'<PASSWORD>hTxFtjVB6',
             'blog':'VmALvQyKAxiVH5G8vo1if1MLZF3sdt',
             'luggage':'12345'}

import sys
if len(sys.argv) < 2:
    print

# !pip install pyperclip

import pyperclip
text = pyperclip.paste()
# Separate lines and add stars.
lines = text.split('\n')
for i in range(len(lines)): # loop through all indexes in the "lines" list
 lines[i] = '* ' + lines[i] # add star to each string in "lines" list
pyperclip.copy(text)

pyperclip='My name is Madhav Hello world'

import pyperclip
text = pyperclip.paste()
print(text)
# Separate lines and add stars.
lines = text.split('\n')
for i in range(len(lines)): # loop through all indexes in the "lines" list
 lines[i] = '* ' + lines[i] # add star to each string in "lines" list
print(lines)
text = '\n'.join(lines)
pyperclip.copy(text)

print(lines)

# ###  Split 

 'My name is Simon'.split()

'MyABCnameABCisABCSimon'.split('ABC')

'My name is Simon'.split('m')

 spam = '''Dear Alice,
How have you been? I am fine.
There is a container in the fridge
that is labeled "Milk Experiment".
Please do not drink it.
Sincerely,
Bob'''
spam.split('\n')

# ### Regular Expressions     

import re

phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')

mo = phoneNumRegex.search('My number is 415-555-4242.')

print('Phone number found: ' + mo.group())

phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')

mo = phoneNumRegex.search('My number is 425-555-4242.')

mo.group(1)

mo.group(2)

mo.group(0)

mo.group()

mo.groups()

# +
areaCode, mainNumber = mo.groups()

print(areaCode)


# -

xmasRegex = re.compile(r'\d+\s\w+')
xmasRegex.findall('12 drummers, 11 pipers, 10 lords, 9 ladies, 8 maids, 7 swans, 6 geese, 5 rings, 3 hens, 2 doves, 1 partridge')

vowelRegex = re.compile(r'[aeiouAEIOU]')

vowelRegex.findall('RoboCop eats baby food. BABY FOOD.')

consonantRegex = re.compile(r'[^aeiouAEIOU]')
consonantRegex.findall('RoboCop eats baby food. BABY FOOD')

# ### >> You can also use the caret symbol (^) at the start of a regex to indicate that a match must occur at the beginning of the searched text
#
# ### >>Likewise, you can put a dollar sign ($) at the end of the regex to indicate the string must end with this regex pattern

beginsWithHello = re.compile(r'^Hello')
beginsWithHello.search('Hello world!')


beginsWithHello.search('He said hello.') == None

# ####   The r'\d$' regular expression string matches strings that end with a numeric character from 0 to 9.

endsWithNumber = re.compile(r'\d$')

endsWithNumber.search('Your number is 42')

endsWithNumber.search('Your number is forty two.')== None

# #### The r'^\d+$' regular expression string matches strings that both begin and end with one or more numeric characters.

wholeStringIsNum = re.compile(r'^\d+$')
wholeStringIsNum.search('1234567890')


wholeStringIsNum.search('12345xyz67890')== None


wholeStringIsNum.search('12 34567890') == None

atRegex = re.compile(r'.at')
atRegex.findall('The cat in the hat sat on the flat mat.')

nameRegex = re.compile(r'First Name: (.*) Last Name: (.*)')
mo = nameRegex.search('First Name: Al Last Name: Sweigart')

mo.group(1)

mo.group(2)

nongreedyRegex = re.compile(r'<.*?>')
mo = nongreedyRegex.search('<To serve man> for dinner.>')
mo.group()

greedyRegex = re.compile(r'<.*>')
mo = greedyRegex.search('<To serve man> for dinner.>')
mo.group()

# <li>(.*) uses greedy mode 
# <li>(.*?) uses nongreedy mode

import pyperclip, re
phoneRegex = re.compile(r'''(
 (\d{3}|\(\d{3}\))? # area code
 (\s|-|\.)? # separator
 (\d{3}) # first 3 digits
 (\s|-|\.) # separator
 (\d{4}) # last 4 digits
 (\s*(ext|x|ext.)\s*(\d{2,5}))? # extension
 )''', re.VERBOSE)

# Create email regex.
emailRegex = re.compile(r'''(
u [a-zA-Z0-9._%+-]+ # username
v @ # @ symbol
w [a-zA-Z0-9.-]+ # domain name
 (\.[a-zA-Z]{2,4}) # dot-something
 )''', re.VERBOSE)

# +
# Find matches in clipboard text.
text = str(pyperclip.paste())

matches = []
for groups in phoneRegex.findall(text):
    phoneNum = '-'.join([groups[1], groups[3], groups[5]])
    if groups[8] != '':
        phoneNum += ' x' + groups[8]
    matches.append(phoneNum)
for groups in emailRegex.findall(text):
    matches.append(groups[0])
# -

# Copy results to the clipboard.
if len(matches) > 0:
    pyperclip.copy('\n'.join(matches))
    print('Copied to clipboard:')
    print('\n'.join(matches))
else:
    print('No phone numbers or email addresses found.')

# +





StarcoderdataJupyterScriptsDedupFiltered