Prepare > Python > Strings > Text Wrap
2023. 7. 10. 22:21ㆍHackerRank-Python
Text Wrap | HackerRank
Wrap the given text in a fixed width.
www.hackerrank.com
문제
You are given a string S and width w.
Your task is to wrap the string into a paragraph of width w.
string: a single string with newline characters ('\n') where the breaks should be
=> 문자열 S와 너비값 w가 주어진다. 길이w의 문단으로 문자열을 랩핑하라
코드
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)
노트
textwrap.wrap(long_text, width=70)
- long_text를 width길이 만큼씩 자름(최대길이가 70)
- 리스트로 리턴
textwrap.fill(long_text, width=70)
- textwrap.wrap()의 결과를 "/n".join(List) 해서 리턴
- 잘린 각 문장들을 줄바꿈하여 출력
참조
파이썬(Python) textwrap 모듈을 이용한 긴 문장(문자열) 정리하기(생략, 줄바꿈).
너무 긴 문장을 변수에 담아서 출력한다면 읽기가 쉽지 않을 것이다. 파이썬(Python)에서 제공하는 내장 모듈 textwrap을 이용하면 긴 문장(문자열) 생략(중략)하거나 줄 바꿈을 해줄 수 있다. textwrap
zephyrus1111.tistory.com
'HackerRank-Python' 카테고리의 다른 글
Prepare > Python > Strings > String Formatting (0) | 2023.07.12 |
---|---|
Prepare > Python > Strings > Designer Door Mat (0) | 2023.07.11 |
Prepare > Python > Strings > Text Alignment (0) | 2023.07.09 |
Prepare > Python > Strings > String Validators (0) | 2023.07.08 |
Prepare > Python > Strings > Find a string (0) | 2023.07.06 |