Unit 5: Strings Flashcards
What is the purpose of string slicing ?
read more than one character at a time. Multiple consecutive characters can be read using slice notation.
Slice notation syntax
my_str[start:end]
Determines how much to increment the index after reading each element
Stride
Stride syntax in slice notation
my_str[start:end:stride]
format specification that defines the minimum number of characters that must be inserted into the string. If the replacement value is smaller in size than the given field width, then the string is padded with space characters.
Field width
Field width syntax
{name:16}
character that determines how a value should be aligned within the width of the field.
Alignment character
alignment character format
{name:>16}
is used to pad a replacement field when the inserted string is smaller than the field width. The default character is an empty space ‘ ‘
Fill character
fill character syntax
{name:0>16}
floating point precision
format specification indicates how many digits should be included in the output of floating types.
floating point precision syntax
{name:0>16.2f}
{variable_name:.2f}
replace(old, new)
Returns a copy of the string with all occurrences of the substring old replaced by the string new. The old and new arguments may be string variables or string literals.
replace(old, new, count)
Same as above, except replace(old, new, count) only replaces the first count occurrences of old.
find(x)
find(x) – Returns the index of the first occurrence of item x in the string, otherwise, find(x) returns -1. x may be a string variable or string literal