Strings & Variables Flashcards
(13 cards)
What should you do if you want to use /output text in Python?
you have to use a string.
How is a string created?
A string is created by entering text between two single or double quotation marks.
What would I call what is inside the quotation marks?
print( ‘Coding in Finance’)
print (“An investment in knowledge pays the best interest.”)
A string
For characters that can’t be directly included in a string. For instance, double quotes can’t be directly included in a double quote string; this would cause it to end prematurely.
What is used?
Characters like these must be escaped by placing a backslash before them.
E.g
print( ‘Brian\’s mother: He\’s not an angel. He\’s a very naughty boy!’)
What represents a new line in Python?
\n represents a new line.
Which of the following options results in exactly two lines?
‘one ' two ' three’
“Hello \n world”
“Hello world”
“Some \n text \n goes here”
“Hello \n world”
How can you make newlines automatically be added?
Newlines will be automatically added for strings that are created using three quotes.
E.g:
print(““this
is a
multiline
text”””)
Output:
this
is a
multiline
text
What is concatenation?
As with integers and floats, strings in Python can be added.
E.g:
print (“Spam”+ ‘eggs’)
Output:
Spameggs
If your strings contain numbers, how are they added?
Even if your strings contain numbers, they are still added as strings rather than integers.
E.g:
print (“2”+”2”)
Output:
22
What happens of you add a string and a number?
Adding a string to a number produces an error, as even though they might look similar, they are two different entities.
What happens if you multiply strings by integers?
Strings can also be multiplied by integers. This produces a repeated version of the original string.
E.g:
print (“spam” * 3)
print (4 * ‘2’)
Output:
spamspamspam
2222
Can strings be multiplied by other strings?
Strings can’t be multiplied by other strings.
Can strings be multiplied by floats?
Strings also can’t be multiplied by floats, even if the floats are whole numbers.