Unit 4 Flashcards
What is the difference between a string variable and a numeric variable?
A string is a collection of one or more letters or characters.—> like a word (Letters go into something go into strings). We put quotations marks around it to let Python expect a string. You need to make sure that the quotation marks matches.
What is the term for combining strings?
Concatenating strings
How can you write code that uses Python’s built-in function to get input from the user (prompt the user to enter data) and then process it for use in your programs?
To get information from the user, we use a function called input()
Let’s say that you have two different variables
Question = …
Response= …
What are the different ways that you can put quotation marks for these two variables?
Question= ‘….’
Response=‘…’
Or
Question= “…”
Response=“…”
Or
Question=‘…’
Response=“…”
Or
Question=“…”
Response=‘…’
Which of the following is an invalid string according to Python?
my_string =This is an invalid “string”.
my_string=“T”
my_string=‘This is an invalid string.’
my_string=“This is an invalid string.”
my_string= This is an invalid “string”.
How can we store two jokes in the same variable, but on two different lines?
We use multi-line strings. You use three single quotation marks (‘’’) around your string and press enter anytime you want to go onto a new line.
For example:
Joke_1=‘’’What did one ocean say to the other?
They wave!’’’
Which of the following correctly creates and prints out a multi-line string?
A:
my_joke=‘What does a computer snack on?
Computer chips!’
print(my_joke)
B:
my_joke=‘’What does a computer snack on?
Computer chips!’’
print(my_joke)
C:
my_joke=‘’’What does a computer snack on?
Computer chips!’’’
print (my_joke)
D:
my_joke= “What does a computer snack on?”
“Computer chips!”
print(my_jokes)
C
What happens if I need to include a quotation mark inside my string?
For example: ‘Why can’t you give Elsa a balloon?’
You will get a syntax error. To prevent getting a syntax error you can either use a triple quotation mark or add a slash \ before the ‘ in can’t to tell Python that it can safely ignore your quotation marks.
What is the definition of syntax?
Syntax is the arrangement and order of words and commands you use to written your program.
Which of the following is an invalid string according to Python?
A:
my_string=‘This isn’t a valid string.’
B:
my_string=‘This isn\’t a valid string.’
C:
my_string=‘’’This isn’t a valid string.’’’
D:
my_string=“ This isn\’t a valid string.”
A
Remember you must use three single quotes. (‘’’) or an escape character (\’) to include a quotation mark inside of a string.
What should the code be to have this as the output?
When the snow is shaken
From the balsam trees
And they’re cut down
And brought into our bourses
-from “Noel” by Anne Porter
Please use comments for your program
Define the poem as a multi-line string using triple quotes (‘’’)
poem=‘’’When the snow is shaken
From the balsam trees
And they’re cut down
And brought into our houses
-from “Noel” by Anne Porter’’’
#Print the poem
print (poem)
What is the tool in python you can use to change or add to a string later on?
String formatting
What is string formatting?
We use a set of curly braces {} to indicate where you’ll fill something in later on. It means that when there’s the curly braces, we’re going to be able to add a string later on. That way we can easily change whatever we are going to code.
What do we type in order to let Python know what string we want to insert?
We use the .format() method on our beginning string. We’ll learn about methods a bit later in the course, but for now, just know that it’s a way we can specify how we want our string to look.
How does string formatting help?
It makes it more convenient and saves time as we do not have to type the whole beginning out into a new variable now. We could use curly braces to indicate that we wanted to put in or insert the contents of the variable …. And the variable of …
Is this statement true or false?
We can only insert one variable into one curly braces for string formatting.
True. Each {} holds one variable.
How can we insert more than one variable for a single string? State an example.
By using multiple pairs of {}
beginning=‘’’Knock knock!
Who’s there?
{}
{} who?’’’
print (beginning.format(joke_1,joke_1))
As you have noticed, you need to type joke_1 twice here to suggest that joke_1 is inserted into the first and second {}
Output:
Knock knock!
Who’s there?
A little old lady
A little old lady who?
How do you multiply strings together?
I don’t think you can multiply strings together only string with an integer. So depending on the integer, your string woud repeat that number of times. To add “white space” to how your string disposals in Python.
For the concept called “white space” is it only for the coding world or the non-coding world?
The concept called “white space” is in both the coding world and the non-coding world.
What is the meaning of white space?
This means space, or room, before or after the other text you’ve written.
Think about a letter you might receive in the mail. Many times, the address line has white space before it to make it line up nicely.
What do you use white space for?
In Python, as we’ll learn later on, you use white space to indicate when certain sections, or blocks of code are supposed to remain together.
Code a program that gives this as the output.
I need some extra space!
Here is some extra space!
extra_spaces=‘ ‘ * 10
print(‘{} I need some extra space!’.format(extra_spaces))
print(‘ {} Here is some extra space!’.format(extra_spaces))
Which of the following correctly prints exactly 45 white space characters before the text?
A:
extra_spaces = “ * 45”
print(‘ {}Exactly 45 spaces!’.format(extra_spaces))
B:
extra_spaces=‘ ‘ * 45
print( ‘ {}Exactly 45 spaces!’.format (extra_spaces))
C:
extra_spaces= ‘’’’ * 45– two different “
print(‘ {} Exactly 45 spaces!’ .format(extra_spaces))
D:
extra_spaces= ‘’’ * 45’’’
print(‘ {} Exactly 45 spaces!’ .format(extra_spaces))
B is the correct answer.
C is incorrect as there is no space between the two double quotation marks. However, if it has a space in between “ “ then it would work.
What is a function?
A function is a block of organised, reusable code that ideally performs a single action.