Python String Manipulation and Graphs Flashcards

1
Q

What is a raw string?

A

Suppresses blackslashes in string.
Make one by putting r before first quote e.g. r”some text”
It’s type is str just like regular strings

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

What is __doc__?

A
-If the first thing in a function or class body is a string literal,
it is stored in the \_\_doc\_\_ attribute of the function/class
-Can also be unicode strings
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What data type does python not have?

A

character

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

Get the length of a string

A

– len(string)

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

Access a character in the string with subscript notation

A

– c = string[1]

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

● Access a substring of a string with slice notation

A

– sub = string[1:5]

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

● Reverse a string by using slice notation with a negative step

A

– rev = string[::-1]

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

● Iterate over the characters of a string

A

– for c in string:

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

Convert a string to an array of strings split on a given

separator

A

– arr = string.split(separator)

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

Concatenate an array of strings into a single string, inserting a separator between each

A

– s = separator.join(Iterable)

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

Standard approach to build a String Character-by-Character

A

The standard approach is to build an array of singlecharacter strings, then use join()

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

What are the string predicates?

A

Test type of characters in string; return true if nonempty and all characters are of the specified type

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

How do you capitalize the first character in a string?

A

cap = string.capitalize() first char uppercased, rest lowercased

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

How do you uppercase->lowercase and vice versa?

A

– swp = string.swapcase()

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

How do you capitalize the first character in every word?

A

– tc = string.title()

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

How to check if a substring is a prefix

A

b = string.startswith(sub)

17
Q

How to check if a substring is a suffix

A

b = string.endswith(sub)

18
Q

How to get the index of a substring (2)

A

– idx = string.find(sub) index of first occurrence of sub or -1
– idx = string.rfind(sub) index of last occurrence of sub or -1

19
Q

How to count number of occurences of substring in a string

A

string.count(sub) (can’t be overlapping)

20
Q

Replace occurrences of a substring with a new string with and without a limit number (2)

A

– rep = string.replace(old,new) all occurrences

– rep = string.replace(old,new,count) at most count occur

21
Q

Partition string around a separator (2)

A

– (before, sep, after) = string.partition(sep) use first occur of sep
– (before, sep, after) = string.rpartition(sep) use last occur of sep
The separator can be a regex!

22
Q

add spaces to center string in width

A

– j = string.center(width)

23
Q

add spaces to start/end as needed

A

– j = string.[lr]just(width)

24
Q

add leading zeros to number as needed

A

– n = string.zfill(width)

25
Q

Remove leading and trailing spaces from string (3)

A

– s = string.strip() remove leading and trailing whitespace
– s = string.lstrip() remove leading whitespace
– s = string.rstrip() remove trailing whitespace

26
Q

use conversion specifier with format function

A

“Hello, {conversion_specifier}”.format(some_number)

27
Q

Conversion specifier for 3 digit zero padded decimal

A

%03d

28
Q

For f string, when is value evaluated?

A

value is evaluated at run-time

29
Q

What are the magic functions for string formatting? (3)

A

-__str__ is called by print() to convert an object to a readable string
– __repr__ converts an object into an unambiguous string representation; it is called by the default __str__() for containers, by print() if __str__ is not defined, and by str.format() and f-strings if the !r conversion specifier is given
– __format__ is called by str.format() and f-strings if formatting options are present

30
Q

Check if string contains only letters or digits

A

– b = string.isalnum()

31
Q

Check if string contains only characters valid for forming a number

A

– b = string.isnumeric()

32
Q

Check if string contains only characters valid for a radix-10 number

A

– b = string.isdecimal()

33
Q

Check if string contains only letter (uppercase or lowercase)

A

– b = string.isalpha()

34
Q

Check if string contains only lowercase letter

A

– b = string.islower()

35
Q

Check if string contains only uppercase letter

A

– b = string.isupper()

36
Q

Check if string contains only digits

A

– b = string.isdigit()

37
Q

Check if string contains only whitespace

A

– b = string.isspace()

38
Q

What type do you use for regex in python?

A

raw strings