Strings Flashcards
Everything is an _
Object
What do isinstance, id and type do? How do you use each?
isinstance - checks if an object type is true:
isinstance(“ur mom”, str)
True
id - returns the id of an object:
id(“ur mom”)
some long ass number representing the id
type - tell u the type of the object
type(“ur mom”)
string
What do \n, \t and \ do? Write out an example on how to use them
\n - creates a new line
print(“I love \n ur mom”)
I love
ur mom
\t - creates a tab
print(“I love \t ur mom”)
I love ur mom”
\ - tells python to not read the next quotation marks as string parameters and instead apart of the string
print(“I don't love ur mom”)
I don’t love ur mom
\ - tells python to not read the next \ as an operator and instead apart of the string
print”(I do \ love ur mom”)
I do \ love ur mom
When can + and * be used in strings?
\+ can be used when adding two strings together str1 = "I love" str2 = "ur mom" str3 = str1 + str3 print(str3) I loveur mom
* can be used to multiply a string by and interger str_1 = "I love ur mom" str_2 = str1*3 print(str_2) I love ur momI love ur momI love ur mom
Strings are immutable. Explain
This means that what’s in string cannot change, u cannot modify a string. If you want to add two strings together u have to create a new variable. If u want to extract things from a list and add to another list, or just extract u can use indexing [ : ] or [ : : ]. First one goes start, stop, where stop is one after where we want to stop. Second one goes start, stop, step where step is counting every nth index. Indexing starts from 0 on the left and -1 on the right.
What is range and how is it used
range assigns a number starting from 0 to a bunch of characters. It is mostly used with len.
example:
range(len(“Sodaba”))
range(0, 6)
you can specify where u want range to start if u don’t want it to start at 0.
range(1, len(“Sodaba”))
range(1, 7)
Can also be used as start stop step