1
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.

2
  • Please provide an example of input data and expected output. Commented May 24, 2020 at 8:50
  • You can use a generator directly in your sample, E.g., [x for x in dir(str) if not "__" in x] Commented May 24, 2020 at 9:16

3 Answers 3

1

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   
Sign up to request clarification or add additional context in comments.

2 Comments

You should provide a greater-than-zero default for max in the second example.
@DanielB Thanks! Indeed it is better for the general case. Added
0

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

2 Comments

You can use enumerate inserted of the counter
added enumerate
0

This is probably the simplest way to add a list item with the string '\n' after every 5 items:

for i in range(5, len(listMethods), 6):
    listMethods.insert(i, '\n')

Is this the functionality that you're looking for?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.