Strings and Variables Flashcards
”, ‘
Used to create a string
ex: “Hello”, ‘345’
\
Used to escape “ and ‘ when appropriate
ex: print(‘I/’m a cat’) –> I’m a cat
\n
Creates new line
ex: print(“Hello\nthere”) –>
Hello
there
\t
Creates tab
ex: print(“Hello\tthere”) –>
Hello there
”””
Automatically puts newlines when used in place of “ or ‘
ex: print(“"”Why,
hello there”””) –>
Why,
Hello there
Concatenation
Adding strings together
ex: print(“Spam” + “Eggs”) –> SpamEggs, print(“1”+”2”+”3”) –> 123
Strings can be multiplied by floats
False, only integers
=
Creates variable
ex: name = “Anna”
Python is a case sensitive languauge
True
What can be used as a name in Python?
Allowed: Letters, Numbers, Underscores
ex: this_is_a_normal_name = 7
You can start a name with a number
False
ex: 123abc = 7 –> “SyntaxError: invalid syntax”
You can reassign a variable as much as you want
True, though not good practice
input()
Function to get input from user
ex: x = input()
Even if the user enters a number as input, it is processed as a string.
True
input(“ “), input(‘ ‘)
Creates prompt message
ex: name = input(“Enter your name: “)
int()
Converts input/variable to an integer
ex: age = int(input())
str()
Converts input/variable to a string
ex: print(“you are now” + str(age))
float()
Converts input/variable to a float
ex: money = float(input())
+=, -=, *=, /=, %=, etc.
In-place operators. Allow you to write code like x= x+3 more concisely
ex: x += 3
:=
Walrus operator, allows you to assign values to variables within an expression, including variables that do not exist yet.
ex: num = int(input())
print(num) –>
print(num:=int(input()))