Advertisement
Guest User

Untitled

a guest
May 9th, 2012
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # P2PU - Python Programming - Chapter 5 - Datatypes
  4. # Chapter 6 - Strings
  5. print 'Python for Informatics - Chapter 6: Strings'
  6.  
  7. # Exercise 6.1
  8. print '\n# Exercise 6.1\n'
  9. inp =  raw_input('Enter a word: ')
  10. index = len(inp) - 1
  11. while index >= 0:
  12.   print inp[index]
  13.   index -= 1
  14.  
  15. # Exercise 6.2
  16. # fruit[:}
  17. # I just tried it out and it returns the complete string.
  18. # If you omit the first index the slice starts at the beginning.
  19. # If you omit the second index, the slice goes to the end.
  20. # So I should have seen it coming.
  21.  
  22. # Exercise 6.3
  23. print '\n# Exercise 6.3\n'
  24. def counter(haystack, needle):
  25.     count = 0
  26.     for letter in haystack:
  27.         if letter == needle:
  28.             count = count + 1
  29.     print count
  30.    
  31. word   = raw_input('Enter a word: ')
  32. letter = raw_input('Letter to search for: ')
  33. counter(word, letter)
  34.  
  35. # Exercise 6.4
  36. print '\n# Exercise 6.4\n'
  37. word   = raw_input('Enter a word: ')
  38. letter = raw_input('Letter to search for: ')
  39. print word.count(letter)
  40.  
  41. # Exercise 6.5
  42. print '\n# Exercise 6.5\n'
  43. inp = raw_input('Press <enter> to continue')
  44. str = 'X-DSPAM-Confidence:  0.8475'
  45. newstr = str[str.find(':')+1:]
  46. print float(newstr)
  47.  
  48. # Exercise 6.6
  49. print '\n# Exercise 6.6\n'
  50. inp = raw_input('Press <enter> to continue')
  51. print '- Using the center method:\n'
  52. i = 0
  53. while i < 15:
  54.     str = '*' * (i * 2 + 1)
  55.     print str.center(70)
  56.     i += 1
  57. print '\n- Using the replace method:\n'
  58. str = 'And then Romeo met Julia.'
  59. print 'Original text:', str
  60. str = str.replace('Romeo', 'Romea')
  61. str = str.replace('Julia', 'Julio')
  62. print 'Replaced text:', str
  63. print '\n- Using the zfill method:\n'
  64. i = 0
  65. while i < 11:
  66.     print repr(i).zfill(3)
  67.     i += 1
  68. print '\n- Using some various methods:\n'
  69. str = 'And then Romeo met Julia.'
  70. print 'Original text  :', str
  71. print 'All uppercase  :', str.upper()
  72. print 'All lowercase  :', str.lower()
  73. print 'Capitalize     :', str.capitalize()
  74. print 'Title          :', str.title()
  75. print 'Swapcase       :', str.swapcase()
  76. print 'Startswith And :', str.startswith('And')
  77. print 'Startswith Or  :', str.startswith('Or')
  78. print 'Endswith Julia :', str.endswith('Julia')
  79. print 'Endswith Julia.:', str.endswith('Julia.')
  80. print 'Count letter t :', str.count('t'), 'times'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement