HackerRank Solution: Text Wrap

Sakshi Singh
1 min readApr 8, 2019

--

(IN PYTHON3)

Without using TextWrap module

def wrap(string, max_width):

l2=[]

i=0

while(i<len(string)):

k = i+max_width

l2.append(string[i:k])

i = i+max_width

s = “”

for i in l2:

s = s+i+”\n”

return s

if __name__ == ‘__main__’:

string, max_width = raw_input(), int(raw_input())

result = wrap(string, max_width)

print result

Using TextWrap Module

import textwrap

def wrap(string, max_width):

wrapper = textwrap.TextWrapper(width=max_width)

s = wrapper.fill(text=string)

return s

if __name__ == ‘__main__’:

string, max_width = raw_input(), int(raw_input())

result = wrap(string, max_width)

print result

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Sakshi Singh
Sakshi Singh

No responses yet

Write a response