Log in
Sign up for FREE
arrow_back
Library

Python KPRIDE: Strings and Casting

star
star
star
star
star
Last updated 9 months ago
17 questions

Learning Objectives:

  • I can cast one data type to another
  • I can manipulate strings
  • I can explain and debug an algorithm/Python Program

Debug

Click on each link and try dragging and dropping code into the correct order
https://7strikelink7.github.io/NormalPythonParsons/parsons/Casting.html

For GCSE, you must know the following for programming:

  • Outputs and Inputs
  • Variables
  • Maths Operators
  • Random numbers
  • Strings and Casting
  • Booleans
  • If, elif, else
  • Nested Ifs
  • For loops
  • While loops
  • Nested loops
  • Arrays/Lists
  • 2D Lists
  • File handling
  • Subprograms(functions/procedures)

Warm-up Practice:
https://type.withcode.uk/python/String_manipulation

Keywords

  1. Integer: a whole number; a number that is not a fraction.
  2. Float / Real: A number with a decimal point
  3. String: A sequence of alphanumeric characters. It can be any letter, number, or symbols.
  4. len(variable): Returns the length of the variable including white spaces.
  5. Variable: A name in memory that has a value that can change when the program is running. e.g. score = 10
  6. Concatenate: Joins strings together
  7. variable[number]: finds the letter in the variable at the position of the number inside the square brackets. 0 is the first letter.

Predict and Run

Look at the code for each question.
What do you think will be the output?
What is the actual output?


Variables that have a value in Speech marks or Quotation marks have a data type called Strings.
1
1
1
1
1
1
1
1
1
1

Investigate

  1. Look at the code on the right.
  2. Run it and see what it does.
  3. Click on the pencil icon at the top to see the code again.
  4. Change the value of myPassword. What happens?
  5. Change the numbers inside the square brackets on line 9

Extend

Help:
print ( ) is used to output text
input( ) is used to get the user to type something

Variables store values that can change.
Name = Value here


String manipulation help:
https://www.w3schools.com/python/python_strings_modify.asp

length of a string: len ( )
splicing a string: variable [ number ]
capitalise: variable.capitalise()
uppercase: variable.upper()
lowercase: variable.lower()
string to integer: int( number here )
integer to string: str( number here )
1
1
1
1
1
1
1
Question 1
1.

What do you think will be the output from the following code:

favouriteLanguage = "Python"
print(favouriteLanguage * 3)

Question 2
2.

What do you think will be the output from the following code:

Compare = "Python" > "Java"
print(Compare)

Question 3
3.

What do you think will be the output from the following code:

pin = "123"
print(pin * 3)

Question 4
4.

What do you think will be the output from the following code:

height = 150
print(height * 2)

Question 5
5.

What do you think will be the output from the following code:

myName = "John"
print ( len (myName))

Question 6
6.

What do you think will be the output from the following code:

myName = "John Doe"
print ( len (myName))

Question 7
7.

What do you think will be the output from the following code:

word = "Python"
print (word[0])

Question 8
8.

What do you think will be the output from the following code:

word = "amy"
print (word.capitalise())

Question 9
9.

What do you think will be the output from the following code:

word = "Python"
print (word.upper())

Question 10
10.

What do you think will be the output from the following code:

word = "Python"
print (word.lower())

Question 11
11.

Create a program that asks the user to enter a word. Output how many characters are in that word.

E.g.
User enters Hello
Output 5

Question 12
12.

Create a program that can work out the area of a rectangle for the user.
It must ask the user to enter a height and width, and store them as variables.
Output the area by multiplying those two numbers.

e.g.
User enters 5
User enters 6
OUTPUT 30cm squared

Question 13
13.

Create a program to output a sentence in all capital letters

e.g. sentence in all caps --->
SENTENCE IN ALL CAPS

Question 14
14.

Create a program that will ask the user for their name.
Print their name with the proper format - The first letter is capitalised
- the rest of the name in lowercase

Question 15
15.

Create a program that has a variable called myString. Give it a value so that it has at least 9 characters.
Output the 1st, 5th, and 9th character.

e.g.
myString = "abracadabra"
Output acb

Question 16
16.

Create a program that asks the user for their first name and their surname.
Output the first letter of their name, and the first two letters of their surname.

E.g.
User enters John
User enters Doe
Output JDo

Question 17
17.

Challenge:
Create a program that asks the user for a long word.
Output the last 5 letters of that word.

(HINT variableName[ - : ]
https://www.freecodecamp.org/news/how-to-substring-a-string-in-python/)