Posts Text Wrap (Python 3)
Post
Cancel

Text Wrap (Python 3)

  • URL : https://www.hackerrank.com/challenges/text-wrap/problem

  • You are given a string s and width w.
  • Your task is to wrap the string into a paragraph of width w.

  • Input Format
    • The first line contains a string, s.
    • The second line contains the width, w.
  • Constraints
    • 0 < len(s) <= 1000
    • 0 < w <= len(s)
  • Output Format
    • Print the text wrapped paragraph.

문제풀이

  • textwrap을 이용하여 주어진 width의 갯수만큼씩 출력하는 함수를 작성
  • textwrap의 fill 메서드를 사용하여 해결
1
2
3
4
5
6
7
8
9
import textwrap

def wrap(string, max_width):
    return textwrap.fill(string, max_width)

if __name__ == '__main__':
    string, max_width = input(), int(input())
    result = wrap(string, max_width)
    print(result)
This post is licensed under CC BY 4.0 by the author.