listMethods = dir(str)
listMethods[ : ] = [x for x in listMethods if not "__" in x]
listMethods contains all the different methods for string class. I want to try and organize it into five columns.
Simply iterate the list in fives, and print each five as a single string separated by spaces:
for i in range(0, len(listMethods), 5):
print(" ".join(listMethods[i: i+5]))
To achieve a more aligned format, you can use format fill with the longest method. Something like:
maxlen = max((len(x) for x in listMethods), default=15)
for i in range(0, len(listMethods), 5):
print(" ".join(f"{method:{maxlen}}" for method in listMethods[i: i+5]))
Gives:
capitalize casefold center count encode
endswith expandtabs find format format_map
index isalnum isalpha isascii isdecimal
isdigit isidentifier islower isnumeric isprintable
isspace istitle isupper join ljust
lower lstrip maketrans partition replace
rfind rindex rjust rpartition rsplit
rstrip split splitlines startswith strip
swapcase title translate upper zfill
Try this.
# listMethods = list('abcdefghijklmnopqrstuvwxyz')
listMethods = [x for x in dir(str) if not x.startswith('__')]
listFinal = []
for i, element in enumerate(listMethods):
listFinal.append(element)
if i % 5 == 0:
listFinal.append('\n')
print(listFinal)
Output:
['capitalize', 'casefold', 'center', 'count', 'encode', '\n', 'endswith', 'expandtabs', 'find', 'format', 'format_map', '\n', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', '\n', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', '\n', 'istitle', 'isupper', 'join', 'ljust', 'lower', '\n', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', '\n', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', '\n', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', '\n', 'title', 'translate', 'upper', 'zfill']
To view results:
print(', '.join(listFinal).replace('\n, ', '\n'))
# Output
capitalize, casefold, center, count, encode,
endswith, expandtabs, find, format, format_map,
index, isalnum, isalpha, isdecimal, isdigit,
isidentifier, islower, isnumeric, isprintable, isspace,
istitle, isupper, join, ljust, lower,
lstrip, maketrans, partition, replace, rfind,
rindex, rjust, rpartition, rsplit, rstrip,
split, splitlines, startswith, strip, swapcase,
title, translate, upper, zfill
[x for x in dir(str) if not "__" in x]