Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
102 views

Python Worksheet 1 Strings and Variables

The document provides examples of Python code to get user input for their first and last name and store it in variables. It then demonstrates how to manipulate and print those variables, such as getting initials, concatenating into a full name, converting to uppercase, and generating a username using parts of the first and last name.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Python Worksheet 1 Strings and Variables

The document provides examples of Python code to get user input for their first and last name and store it in variables. It then demonstrates how to manipulate and print those variables, such as getting initials, concatenating into a full name, converting to uppercase, and generating a username using parts of the first and last name.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Python Worksheet 1

Strings and Variables

Worksheet 1: Using Strings and Variables


Using the basic program below as an example to build on and refer to, complete the challenges to
improve your skills and help commit the syntax to memory.
print ("What is your name?")
firstname = input()
print ("Hello,",firstname)

1. Add three more, very similar lines of code to do the following: (Be careful with the spelling
of the syntax.)

Ask the user to enter their surname

Assign their answer to a new variable called surname

Output “Your surname is” surname

Save and run your new program by pressing F5. Debug it if it contains any errors.

2. Add the following line of code:

print ("Hello,",firstname,surname)

What do think will happen? Run the program to find out.

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.

print("Your initials are:",firstname[0],surname[0])

4. Declare another variable called fullname. Make this equal to firstname + surname. What do
you think the +" " does in the code below?

fullname = firstname +" "+ surname


print(fullname)

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.

You might also like