LESSON: USEFUL STRING OPERATORS
PROGRAMMING EXERCISES
(Requires IDLE 2.7.10)
IDE - Integrated Development Environment, consists of a source code
editor, build automation tools and a debugger.
#1: Extract Email Provider
Define a function extractEmailProvider(email) that takes the email parameter being
Common String Operations
passed and return the email provider. For example,
% - string format operator, example: (%s - string conversion)
extractEmailProvider(test@gmail.com) return gmail.com
VOCABULARY
range slice - Gives the characters from the given range, example:
a= Hello
a[1:3] = ell
#2 Display the Name as Last, First
Define a functionPrintName(name) that takes the user's full name with one
raw_input and display the name as last, first.
split - returns a list of all the words in the string, example:
str="Hello World"
var = str.split(' ');
print var
# prints a list to the screen containing two items ["Hello", "World"]
#3: Count the Capitals
Define a functionCountCapitals(sen) that takes the sen parameter being passed and
returns how many capital letters are in the string. The function should return the
message: " There are x number of capitals in your sentence"
len - returns the number of elements in the list. For example:
str = "Hello"
len(str) returns the value 5
isUpper() - checks whether all the case-based characters (letters) of the
string are uppercase. For example,
str = "HELLO"
str.isUpper()
(there are other functions as well: isLower(), isAlpha())
PROGRAMMING CHALLENGE # 4
(Prerequisite: Completed Practice Makes Perfect)
Longest Word
Using the Python language, have the function LongestWord(sen) take the
sen parameter being passed and return the largest word in the string. If
there are two or more words that are the same length, return the first
word from the string with that length. Ignore punctuation and assume sen
will not be empty.
def LongestWord(sen):
# code goes here
return
# keep this function call here
print LongestWord(raw_input("Enter a Sentence: ")
Examples of Output:
Input = "fun&!! time" Output = "time"
Input = "I love dogs" Output = "love"
#5: Middle
Write a function called middle that takes a list and returns a new list that contains all
but the first and last elements. So middle([1,2,3,4]) should return [2,3].