Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # P2PU - Python Programming - Chapter 5 - Datatypes
- # Chapter 6 - Strings
- print 'Python for Informatics - Chapter 6: Strings'
- # Exercise 6.1
- print '\n# Exercise 6.1\n'
- inp = raw_input('Enter a word: ')
- index = len(inp) - 1
- while index >= 0:
- print inp[index]
- index -= 1
- # Exercise 6.2
- # fruit[:}
- # I just tried it out and it returns the complete string.
- # If you omit the first index the slice starts at the beginning.
- # If you omit the second index, the slice goes to the end.
- # So I should have seen it coming.
- # Exercise 6.3
- print '\n# Exercise 6.3\n'
- def counter(haystack, needle):
- count = 0
- for letter in haystack:
- if letter == needle:
- count = count + 1
- print count
- word = raw_input('Enter a word: ')
- letter = raw_input('Letter to search for: ')
- counter(word, letter)
- # Exercise 6.4
- print '\n# Exercise 6.4\n'
- word = raw_input('Enter a word: ')
- letter = raw_input('Letter to search for: ')
- print word.count(letter)
- # Exercise 6.5
- print '\n# Exercise 6.5\n'
- inp = raw_input('Press <enter> to continue')
- str = 'X-DSPAM-Confidence: 0.8475'
- newstr = str[str.find(':')+1:]
- print float(newstr)
- # Exercise 6.6
- print '\n# Exercise 6.6\n'
- inp = raw_input('Press <enter> to continue')
- print '- Using the center method:\n'
- i = 0
- while i < 15:
- str = '*' * (i * 2 + 1)
- print str.center(70)
- i += 1
- print '\n- Using the replace method:\n'
- str = 'And then Romeo met Julia.'
- print 'Original text:', str
- str = str.replace('Romeo', 'Romea')
- str = str.replace('Julia', 'Julio')
- print 'Replaced text:', str
- print '\n- Using the zfill method:\n'
- i = 0
- while i < 11:
- print repr(i).zfill(3)
- i += 1
- print '\n- Using some various methods:\n'
- str = 'And then Romeo met Julia.'
- print 'Original text :', str
- print 'All uppercase :', str.upper()
- print 'All lowercase :', str.lower()
- print 'Capitalize :', str.capitalize()
- print 'Title :', str.title()
- print 'Swapcase :', str.swapcase()
- print 'Startswith And :', str.startswith('And')
- print 'Startswith Or :', str.startswith('Or')
- print 'Endswith Julia :', str.endswith('Julia')
- print 'Endswith Julia.:', str.endswith('Julia.')
- print 'Count letter t :', str.count('t'), 'times'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement