Python String Methods Flashcards

1
Q

str.capitalize()

A

Return a copy of the string with its first character capitalized and the rest lowercased.

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

str.casefold()

A

Return a casefolded copy of the string. Casefolded strings may be used for caseless matching.

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

str.center(width[, fillchar])¶

A

Return centered in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s).

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

str.count(sub[, start[, end]])

A

Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.

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

str.encode(encoding=’utf-8’, errors=’strict’)

A

Return an encoded version of the string as a bytes object. Default encoding is ‘utf-8’.

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

str.endswith(suffix[, start[, end]])¶

A

Return True if the string ends with the specified suffix, otherwise return False.

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

str.expandtabs(tabsize=8)

A

Return a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size.

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

str.find(sub[, start[, end]])

A

Return the lowest index in the string where substring sub is found within the slice s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.

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

str.format(*args, **kwargs)

A

Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces {}. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument. See Format String Syntax for a description of the various formatting options that can be specified in format strings.

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

str.format_map(mapping)

A

Similar to str.format(**mapping), except that mapping is used directly and not copied to a dict. This is useful if for example mapping is a dict subclass:

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

str.index(sub[, start[, end]])

A

Like find(), but raise ValueError when the substring is not found.

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

str.isalnum()

A

Return True if all characters in the string are alphanumeric and there is at least one character, False otherwise. A character c is alphanumeric if one of the following returns True: c.isalpha(), c.isdecimal(), c.isdigit(), or c.isnumeric().

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

str.isalpha()

A

Return True if all characters in the string are alphabetic and there is at least one character, False otherwise. Alphabetic characters are those characters defined in the Unicode character database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”. Note that this is different from the “Alphabetic” property defined in the Unicode Standard.

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

str.isascii()

A

Return True if the string is empty or all characters in the string are ASCII, False otherwise. ASCII characters have code points in the range U+0000-U+007F.

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

str.isdecimal()

A

Return True if all characters in the string are decimal characters and there is at least one character, False otherwise. Decimal characters are those that can be used to form numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in the Unicode General Category “Nd”.

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

str.isdigit()

A

Return True if all characters in the string are digits and there is at least one character, False otherwise. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers. Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.

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

str.isidentifier()

A

Return True if the string is a valid identifier according to the language definition, section Identifiers and keywords.

Call keyword.iskeyword() to test whether string s is a reserved identifier, such as def and class.

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

str.islower()

A

Return True if all cased characters 4 in the string are lowercase and there is at least one cased character, False otherwise.

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

str.isnumeric()

A

Return True if all characters in the string are numeric characters, and there is at least one character, False otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.

20
Q

str.isprintable()

A

Return True if all characters in the string are printable or the string is empty, False otherwise. Nonprintable characters are those characters defined in the Unicode character database as “Other” or “Separator”, excepting the ASCII space (0x20) which is considered printable. (Note that printable characters in this context are those which should not be escaped when repr() is invoked on a string. It has no bearing on the handling of strings written to sys.stdout or sys.stderr.)

21
Q

str.isspace()

A

Return True if there are only whitespace characters in the string and there is at least one character, False otherwise.

A character is whitespace if in the Unicode character database (see unicodedata), either its general category is Zs (“Separator, space”), or its bidirectional class is one of WS, B, or S.

22
Q

str.istitle()

A

Return True if the string is a titlecased string and there is at least one character, for example uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return False otherwise.

23
Q

str.isupper()

A

Return True if all cased characters 4 in the string are uppercase and there is at least one cased character, False otherwise.

24
Q

str.join(iterable)

A

Return a string which is the concatenation of the strings in iterable. A TypeError will be raised if there are any non-string values in iterable, including bytes objects. The separator between elements is the string providing this method.

25
Q

str.ljust(width[, fillchar])

A

Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s).

26
Q

str.lower()

A

Return a copy of the string with all the cased characters 4 converted to lowercase.

The lowercasing algorithm used is described in section 3.13 of the Unicode Standard.

27
Q

str.lstrip([chars])

A

Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped:

28
Q

static str.maketrans(x[, y[, z]])

A

This static method returns a translation table usable for str.translate().

If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters (strings of length 1) to Unicode ordinals, strings (of arbitrary lengths) or None. Character keys will then be converted to ordinals.

If there are two arguments, they must be strings of equal length, and in the resulting dictionary, each character in x will be mapped to the character at the same position in y. If there is a third argument, it must be a string, whose characters will be mapped to None in the result.

29
Q

str.partition(sep)

A

Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings.

30
Q

str.replace(old, new[, count])

A

Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

31
Q

str.rfind(sub[, start[, end]])

A

Return the highest index in the string where substring sub is found, such that sub is contained within s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure.

32
Q

str.rindex(sub[, start[, end]])

A

Like rfind() but raises ValueError when the substring sub is not found.

33
Q

str.rjust(width[, fillchar])

A

Return the string right justified in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s).

34
Q

str.rpartition(sep)

A

Split the string at the last occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself.

35
Q

str.rsplit(sep=None, maxsplit=-1)

A

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done, the rightmost ones. If sep is not specified or None, any whitespace string is a separator. Except for splitting from the right, rsplit() behaves like split() which is described in detail below.

36
Q

str.rstrip([chars])

A

Return a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped:

37
Q

str.split(sep=None, maxsplit=-1)

A

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).

If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, ‘1,,2’.split(‘,’) returns [‘1’, ‘’, ‘2’]). The sep argument may consist of multiple characters (for example, ‘1<>2<>3’.split(‘<>’) returns [‘1’, ‘2’, ‘3’]). Splitting an empty string with a specified separator returns [’’].

38
Q

str.splitlines([keepends])

A

Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true.

39
Q

str.startswith(prefix[, start[, end]])

A

Return True if string starts with the prefix, otherwise return False. prefix can also be a tuple of prefixes to look for. With optional start, test string beginning at that position. With optional end, stop comparing string at that position.

40
Q

str.strip([chars])

A

Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:

41
Q

str.swapcase()

A

Return a copy of the string with uppercase characters converted to lowercase and vice versa. Note that it is not necessarily true that s.swapcase().swapcase() == s.

42
Q

str.title()

A

Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase.

43
Q

str.translate(table)

A

Return a copy of the string in which each character has been mapped through the given translation table. The table must be an object that implements indexing via __getitem__(), typically a mapping or sequence. When indexed by a Unicode ordinal (an integer), the table object can do any of the following: return a Unicode ordinal or a string, to map the character to one or more other characters; return None, to delete the character from the return string; or raise a LookupError exception, to map the character to itself.

44
Q

str.upper()

A

Return a copy of the string with all the cased characters 4 converted to uppercase. Note that s.upper().isupper() might be False if s contains uncased characters or if the Unicode category of the resulting character(s) is not “Lu” (Letter, uppercase), but e.g. “Lt” (Letter, titlecase).

45
Q

str.zfill(width)

A

Return a copy of the string left filled with ASCII ‘0’ digits to make a string of length width. A leading sign prefix (‘+’/’-‘) is handled by inserting the padding after the sign character rather than before. The original string is returned if width is less than or equal to len(s).