python functions Flashcards

1
Q

remove whitespace from ‘ this sentence right here ‘

A
x = '  this sentence right here '
x = x.strip()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
a string is my_string = 'abcdefg'
get: 
'c'
'cde'
'fg'
'bcdef'
A

my_string[2]
my_string[2:5]
my_string[-2:]
my_string[1:-1]

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

get index within a list of list item x

A

my_list.index()

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

get count of a specific item x within a list

A

my_list.count(x)

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

add an item to list end

A

my_list.append(x)

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

remove first matching item from list

A

my_list.remove(x)

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

reverse a list

A

my_list.reverse()

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

function to remove item from list given the index or you want to remove from end, returning the item from the function

A

my_list.pop(index or if none, removes last item)

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

insert item to list at specific index

A

my_list.insert(index, x)

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

sort a list

A

my_list.sort()

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

uppercase a string

A

my_string.upper()

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

lowercase a string

A

my_string.lower()

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

get count of item in list or substring within string

A

my_string.count(‘x’)

my_list.count(x)

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