Strings Flashcards

1
Q

Convert string to uppercase

A

str.upper()

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

Convert string to lower case

A

str.lower()

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

Check string contains substring

A

‘substring’ in str

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

Length of string

A

len(str)

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

Replace substring with new substring

A

str.replace(‘old’, ‘new’)

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

Split string into list

A

str.split(separator)

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

Convert string to list

A

list(str)

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

Extract substring from string

A

str[start:end]
Start inclusive, end exclusive

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

Remove leading and trailing white spaces

A

str.strip()

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

Check if string starts or ends with substring

A

str.startswith(‘Hello’)
str.endswith(‘World’)

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

Split string around separator

A

str.partition(‘separator’)

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

Add 0s on the left of string

A

str.zfill(5)
Makes string length 5, adding 0s to make it to 5

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

Reverse string

A

str[::-1]

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

Count frequency of characters in string

A

from collections import Counter

Counter(string)

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