Strings Flashcards

1
Q

Everything is an _

A

Object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do isinstance, id and type do? How do you use each?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What do \n, \t and \ do? Write out an example on how to use them

A

\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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When can + and * be used in strings?

A
\+ 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Strings are immutable. Explain

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is range and how is it used

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly