Python Worksheet 1 Strings and Variables
Python Worksheet 1 Strings and Variables
Introduction to Python
1. Add three more, very similar lines of code to do the following: (Be careful with the
spelling of the syntax.)
print ("Hello,",firstname,surname)
What do think will happen? Run the program to find out and print screen below:
3. The following line of code will print the first character in each of the firstname and
surname variables. Python starts counting at 0, so “Tom” would be T=0, o=1 and
m=2.
1
Strings and variables
Introduction to Python
4. Declare another variable called fullname. Make this equal to firstname + surname.
What do you think the +" " does in the code below?
5. The code print(firstname.upper()) would print the user’s name in capital letters. Write
a line of code to print the surname in capitals, followed by the first name.
6. The code len(firstname) would return the number of letters in the user’s first name.
Write some code that would tell the user how many letters were in their surname.
7. Write some code that would create a new variable to hold a username for a login
system. The username must be created from the first three letters of the user’s
surname, followed by their first initial, followed by the length of their surname. E.g.
Jack Smith would have the username smij5. Hint: Use str(len(surname)) for this
exercise.
2
Strings and variables
Introduction to Python