Skip to content

Instantly share code, notes, and snippets.

@StaticallyTypedRice
Last active May 26, 2018 02:34
Show Gist options
  • Save StaticallyTypedRice/6e5d412926ac2a190b61fc8b510d1bdf to your computer and use it in GitHub Desktop.
Save StaticallyTypedRice/6e5d412926ac2a190b61fc8b510d1bdf to your computer and use it in GitHub Desktop.
A Python function for dividing a single-line string into rows of a specified length.
def split_string_into_rows(string: str, row_length: int = 10) -> str:
''' Splits a string into rows of a specified length.
Rows are separated by \n characters.
NOTE: This function may break if there are already \n characters in the string.
'''
# The character separating the rows
added_character = '\n'
# The rows are stored in this variable
rows = []
# Iterate leter by letter through the string, appending every `row_length`
# letters to a new row
letter = 0
row = ''
for s in string:
if letter < row_length:
# If the letter count is less than the row length:
# Append the current letter
# Add 1 to the letter count
row = row + s
letter = letter + 1
else:
# If the letter count is equal to the row length:
# Append the completed row to the `rows` list
# Set the current letter as the first letter in the next row
# Reset the letter count (to 1 instead of 0 because the first letter was added).
rows.append(row)
row = s
letter = 1
# Once the initial for-loop ends, append the final remaining row to the `rows` list
# As long as it is not empty
if row:
rows.append(row)
# Concatenate all of the rows, adding line break characters
# The reason this is done separately and not during the initial iteration of the string
# is to allow for additional operations on the `rows` list, or for an option to return
# the `rows` list itself instead of a string.
result = ''
for r in rows:
result = result + r + added_character
return result
'''LICENSE:
The MIT License (MIT)
Copyright (c) 2018 Richie Zhang
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment