String methods Flashcards
Return a copy of the string with its first character capitalized and the rest lowercased.
str.capitalize()
What does str.center(width[, fillchar]) ?
Return centered in a string of length width. Padding is done using the specified fillchar (default is a space).
>>> 'text'.center(10) >>> 'text'.center(10, '#') '###text###' >>> 'text'.center(11, '#') '####text###'
How to count char / substring in str?
str.count(sub[, start[, end]])
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 to decode / encode?
str. decode([encoding[, errors]])
str. encode([encoding[, errors]])
Encodings: ‘utf-8’, ‘ascii’
Check if str ends with given suffix.
str.endswith(suffix[, start[, end]]) # returns True or False
How to replace \t tabs with given indend size?
> > > ‘01\t012\t0123\t01234’.expandtabs() # default is 8
‘01 012 0123 01234’
‘01\t012\t0123\t01234’.expandtabs(4)
‘01 012 0123 01234’
Find substring in the given str.
str.find(sub[, start[, end]])
Returns an index of the first substring occurrence. -1 if not found.
To check if sub is a substring or not, don’t use .find, use the in operator:
> > > ‘Py’ in ‘Python’
True
What’s the diff between str.index and str.find methods?
They’re almost the same, but index also raises ValueError when the substring is not found. Find returns -1 if nothing found.
How to format strings with .format?
Args are called by indices. Your kwargs can be **any_dict or keyworded args, they can be called by the keys.
> > > ‘{0} {1} {a}’.format(‘o’, ‘one1’, **{‘a’: ‘a char’})
‘o one1 a char’
> > > ‘{0} {1} {a}’.format(‘o’, ‘one1’, a=’a char’)
‘o one1 a char’
for format tricks see separate deck for it!
How to check if there is at least one character and all chars are alphabetic?
str.isalpha() # True / False
How to check if there is at least one character and all chars are digits?
str.isdigit() # True / False
How to check if there is at least one character and all chars are lowercase?
str.islower() # True / False
How to check if there is at least one character and all chars are whitespaces?
str.isspace()
How to check if there is at least one character and the string is title-cased (first letter in every word is upper-case, all next - lower-case)?
str.istitle()
How to check if there is at least one character and all chars are uppercase?
str.isupper()
Concatenate / build the string from the iterable.
separator_str.join(iterable)
> > > l = [‘1’,’2’,’3’]
‘, ‘.join(l)
‘1, 2, 3’
How to get left-justified string?
str.ljust(width[, fillchar])
Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than or equal to len(s)
> > > info = {‘name’:’nick’, ‘surname’:’bulow’, ‘age’:’153’}
for k, v in info.items(): print k.ljust(10), v
“age 153
surname bulow
name nick”
brainscape font is not fixed width, so output example is broken. second column should have straight order.
How to get right-justified string?
str.rjust(width[, fillchar])