Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Exercise 6.3 Encapsulate this code in a function named count, and generalize it so that it
- accepts the string and the letter as arguments.
- '''
- def count(w,l):
- count = 0
- for letter in w:
- if letter == l:
- count = count + 1
- print count
- count('abbcccddddeeeeeffffff','c')
- '''
- Exercise 6.4 There is a string method called count that is similar to the function in the previous
- exercise. Read the documentation of this method at docs.python.org/library/string.
- html and write an invocation that counts the number of times the letter a occurs in 'banana'.
- '''
- print str.count('banana','a')
- '''
- Exercise 6.5 Take the following Python code that stores a string:‘
- str = ’X-DSPAM-Confidence:0.8475’
- Use find and string slicing to extract the portion of the string after the colon character and then use the float function to convert the extracted string into a floating point number.
- '''
- s ='X-DSPAM-Confidence:0.8475'
- a=s.find(':')
- b=s[a+1:len(s)]
- print b
- '''
- Exercise 6.6 Read the documentation of the string methods at docs.python.org/lib/
- string-methods.html. You might want to experiment with some of them to make sure you
- understand how they work. strip and replace are particularly useful.
- from string import strip, replace
- '''
- s='softvision'
- print strip(s,'soft')
- print replace(s,'soft','hard')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement