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.
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.
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
Code:
myPassword = "aBc123"
print("PASSWORD ANALYSIS...")
print("Length of password...", len(myPassword))
print("Current Password: ", myPassword)
print("Lowercase:", myPassword.lower())
print("Uppercase:", myPassword.upper())
print("Capitalised:", myPassword.capitalize()) # American Spelling with z used for code.
print("First Character:", myPassword[0])
print("First three characters:", myPassword[0:3])
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 )
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
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
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:
Compare = "Python" > "Java"
print(Compare)
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:
height = 150
print(height * 2)
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 Doe"
print ( len (myName))
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 = "amy"
print (word.capitalise())
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.lower())
Create a program to output a sentence in all capital letters
e.g. sentence in all caps --->
SENTENCE IN ALL CAPS
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 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 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
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/)