Advertisement
Guest User

Untitled

a guest
Jul 10th, 2012
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2. Exercise 6.3 Encapsulate this code in a function named count, and generalize it so that it
  3. accepts the string and the letter as arguments.
  4. '''
  5. def count(w,l):
  6.     count = 0
  7.     for letter in w:
  8.         if letter == l:
  9.             count = count + 1
  10.     print count
  11. count('abbcccddddeeeeeffffff','c')
  12. '''
  13. Exercise 6.4 There is a string method called count that is similar to the function in the previous
  14. exercise. Read the documentation of this method at docs.python.org/library/string.
  15. html and write an invocation that counts the number of times the letter a occurs in 'banana'.
  16. '''
  17. print str.count('banana','a')
  18. '''
  19. Exercise 6.5 Take the following Python code that stores a string:‘
  20. str = ’X-DSPAM-Confidence:0.8475’
  21. 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.
  22. '''
  23. s ='X-DSPAM-Confidence:0.8475'
  24. a=s.find(':')
  25. b=s[a+1:len(s)]
  26. print b
  27. '''
  28. Exercise 6.6 Read the documentation of the string methods at docs.python.org/lib/
  29. string-methods.html. You might want to experiment with some of them to make sure you
  30. understand how they work. strip and replace are particularly useful.
  31. from string import strip, replace
  32. '''
  33. s='softvision'
  34. print strip(s,'soft')
  35. print replace(s,'soft','hard')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement