Python KPRIDE: Strings and Casting

Last updated 6 months ago
17 questions

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

Learning Objectives:

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

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

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

favouriteLanguage = "Python"
print(favouriteLanguage * 3)

1

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

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

1

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

pin = "123"
print(pin * 3)

1

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

height = 150
print(height * 2)

1

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

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

1

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

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

1

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

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

1

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

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

1

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

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

1

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

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

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

Debug

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

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

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

1

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

1

Create a program to output a sentence in all capital letters

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

1

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

1

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

1

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

1

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/)