Python KPRIDE: Strings and Casting
star
star
star
star
star
Last updated 6 months ago
17 questions
Keywords
- Integer: a whole number; a number that is not a fraction.
- Float / Real: A number with a decimal point
- String: A sequence of alphanumeric characters. It can be any letter, number, or symbols.
- len(variable): Returns the length of the variable including white spaces.
- Variable: A name in memory that has a value that can change when the program is running. e.g. score = 10
- Concatenate: Joins strings together
- 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)
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)
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)
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 = 150print(height * 2)
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))
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))
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])
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())
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())
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())
What do you think will be the output from the following code:
word = "Python"
print (word.lower())
Investigate
- Look at the code on the right.
- Run it and see what it does.
- Click on the pencil icon at the top to see the code again.
- Change the value of myPassword. What happens?
- 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
Create a program that asks the user to enter a word. Output how many characters are in that word.
E.g.User enters HelloOutput 5
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 5User enters 6OUTPUT 30cm squared
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
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
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
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 JohnUser enters DoeOutput JDo
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/)
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/)