External Exam Download Resources Web Applications Games Recycle Bin

String Manipulation

string manipulation.py

word = "Australia"
#[start:stop:step]
print(word[0]) #A
print(word[0:2]) #Au
print(word[0::2]) #every 2nd letter till the end
print(word[::-1]) #step backwards, from start to end

print(word.isupper()) #is upper-case = False
print("ABC".isupper()) #True
print("abc".islower()) #True

print(word.upper()) #AUSTRALIA 
print(word[::-1].upper()) #AILARTSUA
print(word.lower()) #australia

if "Aust" in word: 
	print("'Aust' found.") 

if "xyz" not in word: 
	print("'xyz' has not been found.")

if word in word: 
	print("fancy that.")

print(len(word)) #9 (i.e. length of word)
print(len("a b c")) #5 - includes spaces
print(word.count("a")) #2
print(word.lower().count("a")) #3 - no more capitals
print(word.replace("a","z")) #Austrzliz


#down the page:
for eachLetter in "Australia":
	print(eachLetter)

#across the page (no need for this, but yolo):
print("".join(c for c in "Australia"))
#^^print(item, sep=' ', end='', flush=True) in Python 3


  1. develop some code that inputs a sentence and checks if it is in CAPITAL LETTERS. If it is, output "calm down mate".

  2. make a program that inputs a sentence and looks for the word "bridge". e.g. "Story Bridge", "wooden bridge". if it finds the word bridge, your program should say "there is a bridge in that sentence."

  3. create a secret decoder where the user inputs a word and the program reads every 3rd letter. for example, inputting "azhmnebbloplggo" outputs "hello".

  4. Write a sentence checker that reads in a sentence, and returns the number of each of the vowels in the sentence. The code should work regardless of capital or lowercase letters. For example, for the sentence "I love Australia", the program will display:
    
    a: 3 
    e: 1 
    i: 2 
    o: 1 
    u: 1

  5. A discussion on ASCII values can be found here. To convert from ASCII character to ASCII code in Python, use ord("A") where "A" is the character i am keen to get the code of (in this case it will be 65). To convert from ASCII value to character, use chr(65). Can you write an encoder / decoder that converts a sentence to ASCII code, and back again?
    Challenge: add an arbitrary value to the ASCII code so you can really hide what you are saying! (remember to minus this arbitrary value when decoding. you could even add or subtract a random arbitrary value (such as todays date as an integer), include this in the string, and use this as a 'key' to decode the message.. which has the makings of encryption science)