0% found this document useful (0 votes)
119 views

Python Worksheet 1 Strings and Variables

The document provides examples of code snippets to practice using strings and variables in Python. It asks the user to input their name and surname, then print and manipulate the strings in various ways such as concatenating, accessing characters by index, checking length, and creating a username from parts of the name strings.

Uploaded by

lucybaik24
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views

Python Worksheet 1 Strings and Variables

The document provides examples of code snippets to practice using strings and variables in Python. It asks the user to input their name and surname, then print and manipulate the strings in various ways such as concatenating, accessing characters by index, checking length, and creating a username from parts of the name strings.

Uploaded by

lucybaik24
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Strings and variables

Introduction to Python

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.

Print screen your code here:

2. Add the following line of code:

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.

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

Run the program and print screen below:

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?

fullname = firstname +" "+ surname


print(fullname)

Run the program and print screen 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.

Print screen your code here:

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.

Print screen your code here:

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.

Print screen your code here:

Run the program and print screen below:

2
Strings and variables
Introduction to Python

You might also like