Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pattern based Python question for beginners

https://stackoverflow.com/questions/58997775/python3-writing-numbers-like-a-table-as-taking-input/58998185#58998185

def get_pattern(n=3, separator='  '):
    a = 0
    pattern = ''

    for i in range(0, n):
        for j in range(0, n):
            pattern += str(a) + separator
            a += 1
        pattern = pattern.rstrip(separator) + '\n'

    return pattern
# -- Call 1 --
pattern = get_pattern()
print(pattern)
# 0  1  2
# 3  4  5
# 6  7  8

# -- Call 2 --
pattern = get_pattern(separator='__')
print(pattern)
# 0__1__2
# 3__4__5
# 6__7__8

# -- Call 3 --
pattern = get_pattern(separator='__', n=5)
print(pattern)
# 0__1__2__3__4
# 5__6__7__8__9
# 10__11__12__13__14
# 15__16__17__18__19
# 20__21__22__23__24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.