Strings - Answers

Q1. A string is any number of characters from the keyboard.
The empty string is a string with no characters in it. It is still an object that has properties.
If you try to give a string value to a variable using myWord1 = Australia you will get an error because the computer thinks Australia is a variable. It should have been put in single or double quotes.

Q4. Get the code working:

1    myWord1 = input('Please enter a word >>> ')
2    print('The length of your word is',len(myWord1))
3    print('The letters in your word are as follows:')
4    for letter in myWord1:
5        print (letter)

Q5. Ask the user for a second word. Then make a new string out of your two words by concatenating them. Finally, print out the length of the new string and all of the characters in the string. Q6. Annotate your program. 

#Get the first word
myWord1 = input('Please enter your first word >>> ')
print('The length of your first word is',len(myWord1))
print('The letters in your word are as follows:')
for letter in myWord1:
    print (letter)

#Get the second word
myWord2 = input('Please enter your second word >>> ')
print('The length of your second word is',len(myWord2))
print('The letters in your word are as follows:')
for letter in myWord2:
    print (letter)

#Concatenate the words
myWord3 = myWord1 + myWord2

#Print out the details of the new string
print('The length of your word is',len(myWord3))
print('The letters in your word are as follows:')
for letter in myWord3:
    print (letter)

Q7. Just because you get results, doesn't mean a program works! Therefore, you should always predict the results you are going to get before actually carrying out the tests. Then you should compare your predictions with what you actually got. A common error amongst students is to just run the code, and when they get results, they say it works.

A range of tests should have been carried out to fully test this program. These include using two valid words from a dictionary, one word and the empty string, two empty strings and using words that do not contain any letters.

Q8. Data type char is simply considered by Python to be a string of length one.