Python - String Methods (Reversed) Flashcards

all string methods, what they do, their return values, and their input arguments

1
Q

string.capitalize()

A

Return - string
Args - none
___________
returns a string with first letter capitalized and all other characters lowercased. It doesn’t modify the original string

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

string.center()

A

Return - string
Args - (width, ‘some char’)
___________
returns a string padded on both sides with specified char. It doesn’t modify the original string. If no second arg provided then blank spaces are default.

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

string.casefold()

A

Return - string
Args - none
___________
removes all case distinctions present in a string. It is used for caseless matching, i.e. ignores cases when comparing. For example, German lowercase letter ß is equivalent to ss. However, since ß is already lowercase, lower() method does nothing to it. But, casefold() converts it to ss.

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

string.count()

A

Return - int
Args - (substring, start, end)
___________
searches the substring in the given string and returns how many times the substring is present in it. It also takes optional parameters start and end to specify the starting and ending positions in the string respectively.

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

string.endswith()

A

Return - True/False
Args - (suffix, start, end)
___________
returns True if a string ends with the specified suffix. If not, it returns False.

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

string.expandtabs()

A

Return - copy of string
Args - (tab size)
___________
returns a copy of string with all tab characters ‘\t’ replaced with whitespace characters until the next multiple of tabsize parameter.

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

string.encode()

A

Return - byte string
Args - (encoding=’UTF-8’,errors=’strict’)
___________
By default, encode() method doesn’t require any parameters.

It returns utf-8 encoded version of the string. In case of failure, it raises a UnicodeDecodeError exception.

However, it takes two parameters:

1) encoding - the encoding type a string has to be encoded to
2) errors - response when encoding fails.
There are six types of error response:

strict - default response which raises a UnicodeDecodeError exception on failure

ignore - ignores the unencodable unicode from the result

replace - replaces the unencodable unicode to a question mark ?

xmlcharrefreplace - inserts XML character reference instead of unencodable unicode

backslashreplace - inserts a \uNNNN espace sequence instead of unencodable unicode

namereplace - inserts a \N{…} escape sequence instead of unencodable unicode

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

string.find()

A

Return - index
Args - (substring, start, end)
___________
returns the index of first occurrence of the substring (if found). If not found, it returns -1. Start and End args are optional.

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

“blah blah {0} blah blah {1:5.3f}”.format(‘input0’, ‘input2’)

A

Return - formatted string with inputs
Args - (first input, second input, …)
___________
reads the type of arguments passed to it and formats it according to the format codes defined in the string. First value in given string is the argument it references and will substitute for in the given parameters, first number after colon is the number of total spaces allocated to the entire inputted argument, number after decimal with the f is the number of decimal places after the input number

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

string.index()

A

Return - index
Args - (substring, start, end)
___________
returns the index of a substring inside the string (if found). If the substring is not found, it raises an exception

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

string.isalnum()

A

Return - True/False
Args - none
___________
returns True if all characters in the string are alphanumeric (either alphabets or numbers). If not, it returns False

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

string.isalpha()

A

Return - True/False
Args - none
___________
returns True if all characters in the string are alphabets. If not, it returns False.

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

string.isdecimal()

A

Return - True/False
Args - none
___________
returns True if all characters in a string are decimal characters. If not, it returns False.

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

string.isdigit()

A

Return - True/False
Args - none
___________
returns True if all characters in a string are digits. If not, it returns False

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

string.isidentifier()

A

Return - True/False
Args - none
___________
returns True if the string is a valid identifier in Python. If not, it returns False.

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

string.islower()

A

Return - True/False
Args - none
___________
returns True if all alphabets in a string are lowercase alphabets. If the string contains at least one uppercase alphabet, it returns False.

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

string.isnumeric()

A

Return - True/False
Args - none
___________
returns True if all characters in a string are numeric characters. If not, it returns False.

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

string.isprintable()

A

Return - True/False
Args - none
___________
returns True if all characters in the string are printable or the string is empty. If not, it returns False.

Characters that occupies printing space on the screen are known as printable characters. For example:

letters and symbols
digits
punctuation
whitespace

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

string.isspace()

A

Return - True/False
Args - none
___________
returns True if there are only whitespace characters in the string. If not, it return False.

Characters that are used for spacing are called whitespace characters. For example: tabs, spaces, newline etc.

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

string.istitle()

A

Return - True/False
Args - none
___________
returns True if the string is a titlecased string. If not, it returns False.

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

string.isupper()

A

Return - True/False
Args - none
___________
returns whether or not all characters in a string are uppercased or not.

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

separator = ‘, ‘

separator.join(someIterable)

A

Return - concatenated string
Args - (iterable)
___________
provides a flexible way to concatenate string. It concatenates each element of an iterable (such as list, string and tuple) to the string and returns the concatenated string

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

string.ljust() and rjust()

A

Return - string
Args - (width, ‘optional fill char’)
___________
returns a left or right-justified string of a given minimum width.

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

string.lower()

A

Return - lowercased version of string
Args - none
___________
converts all uppercase characters in a string into lowercase characters and returns it.

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

string.upper()

A

Return - uppercased version of string
Args - none
___________
converts all lowercase characters in a string into uppercase characters and returns it.

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

string.swapcase()

A

Return - opposite-cased version of string
Args - none
___________
converts all uppercase characters to lowercase and all lowercase characters to uppercase characters of the given string, and returns it.

27
Q

string.lstrip() and rstrip()

A

Return - stripped string
Args - (‘char’) or ([char1, char2, …])
___________
removes characters from the leading left or right based on the argument (a string specifying the set of characters to be removed).

28
Q

string.strip()

A

Return - stripped string
Args - (‘char’) or ([char1, char2, …])
___________
removes characters from both left and right based on the argument (a string specifying the set of characters to be removed).

The strip() returns a copy of the string with both leading and trailing characters stripped.

When the combination of characters in the chars argument mismatches the character of the string in the left, it stops removing the leading characters.
Similarly, when the combination of characters in the chars argument mismatches the character of the string in the right, it stops removing the trailing characters.

29
Q

string.partition()

A

Return - 3-tuple
Args - (separator)
___________
splits the string at the first occurrence of the argument string and returns a tuple containing the part the before separator, argument string and the part after the separator.

The partition method returns a 3-tuple containing:

the part before the separator, separator parameter, and the part after the separator if the separator parameter is found in the string
string itself and two empty strings if the separator parameter is not found

30
Q

string.maketrans()

A

Return - dict
Args - (x, y, z)
___________
returns a mapping table for translation usable for translate() method.

In simple terms, the maketrans() method is a static method that creates a one to one mapping of a character to its translation/replacement.

It creates a Unicode representation of each character for translation.

This translation mapping is then used for replacing a character to its mapped character when used in translate() method.

3 parameters:

x - If only one argument is supplied, it must be a dictionary.
The dictionary should contain 1-to-1 mapping from a single character string to its translation OR a unicode number (97 for ‘a’) to its translation.
y - If two arguments are passed, it must be two strings with equal length.
Each character in the first string is a replacement to its corresponding index in the second string.
z - If three arguments are passed, each character in the third argument is mapped to None.

31
Q

string.rpartition()

A

Return - 3-tuple
Args - (separator)
___________
splits the string at the last occurrence of the argument string and returns a tuple containing the part the before separator, argument string and the part after the separator.

The rpartition method returns a 3-tuple containing:

the part before the separator, separator parameter, and the part after the separator if the separator parameter is found in the string
two empy strings, followed by the string itself if the separator parameter is not found

32
Q

string.translate()

A

Return - string
Args - (table)
___________
returns a string where each character is mapped to its corresponding character in the translation table.

The translate() method takes a single parameter:

table - a translation table containing the mapping between two characters; usually created by maketrans()

33
Q

string.replace()

A

Return - string
Args - (old substring, new substring, number of times you want it replaced with)
___________
returns a copy of the string where all occurrences of a substring is replaced with another substring.

34
Q

string.rfind()

A

Return - index
Args - (substring, start, end)
___________
returns the highest index of the substring (if found). If not found, it returns -1.

The rfind() method takes maximum of three parameters:

sub - It’s the substring to be searched in the str string.
start and end (optional) - substring is searched within str[start:end]

35
Q

string.rindex()

A

Return - index
Args - (substring, start, end)
___________
returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an exception.

The rindex() method takes three parameters:

sub - substring to be searched in the str string.
start and end(optional) - substring is searched within str[start:end]

36
Q

string.split()

A

Return - list of strings
Args - (separator, max # of splits desired)
___________
breaks up a string at the specified separator and returns a list of strings.

The split() method takes maximum of 2 parameters:

separator (optional)- The is a delimiter. The string splits at the specified separator.

If the separator is not specified, any whitespace (space, newline etc.) string is a separator.

maxsplit (optional) - The maxsplit defines the maximum number of splits.

The default value of maxsplit is -1, meaning, no limit on the number of splits.

37
Q

string.rsplit()

A

Return - list of strings
Args - (separator, max # of splits desired)
___________
splits string from the right at the specified separator and returns a list of strings.

The rsplit() method takes maximum of 2 parameters:

separator (optional)- The is a delimiter. The rsplit() method splits string starting from the right at the specified separator.

If the separator is not specified, any whitespace (space, newline etc.) string is a separator.

maxsplit (optional) - The maxsplit defines the maximum number of splits.

The default value of maxsplit is -1, meaning, no limit on the number of splits.

38
Q

string.splitlines()

A

Return - list of strings
Args - (optional keepends=True)
___________
splits the string at line breaks and returns a list of lines in the string.

The splitlines() takes maximum of 1 parameter.

keepends (optional) - If keepends is provided and True, line breaks are also included in items of the list.

39
Q

string.startswith()

A

Return - boolean
Args - (prefix, start, end)
___________
returns True if a string starts with the specified prefix(string). If not, it returns False.

The startswith() method takes maximum of three parameters:

prefix - String or tuple of strings to be checked
start (optional) - Beginning position where prefix is to be checked within the string.
end (optional) - Ending position where prefix is to be checked within the string.

40
Q

string.title()

A

Return - string
Args - none
___________
returns a string with first letter of each word capitalized; a title cased string.

41
Q

string.zfill()

A

Return - string
Args - (width)
___________
returns a copy of the string with ‘0’ characters padded to the left.

The width specifies the length of the returned string from zfill() with ‘0’ digits filled to the left.

42
Q

string.format_map()

A

Return - string
Args - ( dictionary of mapped key-values )
___________
similar to str.format(mapping) except that str.format(mapping) creates a new dictionary whereas str.format_map(mapping) doesn’t.

43
Q

string.any()

A

Return - boolean
Args - ( iterable )
___________
returns True if any element of an iterable is True. If not, any() returns False. If empty, returns returns False.

44
Q

string.all()

A

Return - boolean
Args - ( iterable )
___________
returns True when all elements in the given iterable are true. If not, it returns False. If empty, returns True.

45
Q

string.ascii()

A

Return - string
Args - ( object like string, list, etc )
___________
returns a string containing a printable representation of an object. It escapes the non-ASCII characters in the string using \x, \u or \U escapes.

46
Q

string.bool()

A

Return - boolean
Args - ( value )
___________
converts a value to Boolean (True or False) using the standard truth testing procedure.

The following values are considered false in Python:

None

False

Zero of any numeric type. For example, 0, 0.0, 0j

Empty sequence. For example, (), [], ‘’.

Empty mapping. For example, {}

objects of Classes which has __bool__() or __len()__ method which returns 0 or False

All other values except these values are considered true.

47
Q

string.complex()

A

Return - complex #
Args - ( string with real and imag parts )
___________
returns a complex number when real and imaginary parts are provided, or it converts a string to a complex number.

The string passed to the complex() should be in the form real+imagj or real+imagJ

In general, the complex() method takes two parameters:

real - real part. If real is omitted, it defaults to 0.
imag - imaginary part. If imag is omitted, it default to 0.
If the first parameter passed to this method is a string, it will be interpreted as a complex number. In this case, second parameter shouldn’t be passed.

48
Q

enumerate()

A

Return - tuples of (counter, iterable)
Args - ( iterable, optional start value )
___________
adds counter to an iterable and returns it (the enumerate object).

The enumerate() method takes two parameters:

iterable - a sequence, an iterator, or objects that supports iteration
start (optional) - enumerate() starts counting from this number. If start is omitted, 0 is taken as start.

49
Q

filter()

A

Return - an iteratOR
Args - ( function, iterable )
___________
constructs an iterator from elements of an iterable for which a function returns true.

filters the given iterable with the help of a function that tests each element in the iterable to be true or not.

50
Q

float()

A

Return - float
Args - ( optional string to convert )
___________
returns a floating point number from a number or a string.

The float() method takes a single parameter:

x (Optional) - number or string that needs to be converted to floating point number
If it’s a string, the string should contain decimal points

51
Q

input()

A

Return - string
Args - ( optional string to prompt user )
___________
reads a line from input, converts into a string and returns it.

52
Q

int()

A

Return - int object
Args - ( string to convert, optional base )
___________
returns an integer object from any number or string.

he int() method returns:

an integer object from the given number or string, treats default base as 10
(No parameters) returns 0
(If base given) treats the string in the given base (0, 2, 8, 10, 16)

53
Q

iter()

A

Return - iter object
Args - ( set/tuple object to turn into iter, optional sentinel value to represent end of sequence )
___________
creates an object which can be iterated one element at a time.

These objects are useful when coupled with loops like for loop, while loop.

54
Q

len()

A

Return - length
Args - ( string/bytes/tuple/list/range )
___________
returns the number of items (length) in an object.

55
Q

max()

A

Return - largest element
Args - ( iterable, optional second iterable to compare against one-by-one, etc )
___________
returns the largest element in an iterable or largest of two or more parameters.

56
Q

min()

A

Return - smallest element
Args - ( iterable, optional second iterable to compare against one-by-one, etc )
___________
returns the smallest element in an iterable or smallest of two or more parameters.

57
Q

map()

A

Return - map object
Args - ( function, iterable )
___________
applies a given function to each item of an iterable (list, tuple etc.) and returns a map of the results.

this map can then be wrapped by the list() method to create a list of the results, or wrapped by a set() method, etc

58
Q

ord()

A

Return - int representing unicode
Args - ( char )
___________
returns an integer representing Unicode code point for the given Unicode character.

59
Q

reversed()

A

Return - sequence
Args - ( tuple/string/list/range )
___________
returns the reversed iterator of the given sequence.

60
Q

slice()

A

Return - slice object
Args - ( optional start, stop, optional step )
___________
creates a slice object representing the set of indices specified by range(start, stop, step).

The slice object is used to slice a given sequence (string, bytes, tuple, list or range) or any object which supports sequence protocol (implements __getitem__() and __len__() method).

61
Q

sorted()

A

Return - sorted list
Args - ( iterable, optional reverse=True, optional function that serves as a key to the sort comparison )
___________
returns a sorted list from the given iterable.

62
Q

sum()

A

Return - sum
Args - ( iterable, optional start value )
___________
adds the items of an iterable and returns the sum.

63
Q

zip()

A

Return - iterator of tuples
Args - ( 1st iterable, 2nd iterable, etc)
___________
take iterables (can be zero or more), makes iterator that aggregates elements based on the iterables passed, and returns an iterator of tuples.