Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Visual Studio
- VS Code
- 오류
- C
- C#
- label
- 프로그래머스
- pandas
- Numpy
- 기타 연주
- C++
- SSH
- 컨테이너
- Selenium
- paramiko
- OpenCV
- windows forms
- pip
- ubuntu
- Python
- YOLO
- 핑거스타일
- mysql
- error
- 채보
- LIST
- JSON
- pytorch
- Docker
- Linux
Archives
- Today
- Total
기계는 거짓말하지 않는다
Python 여러 줄 문자열 각 라인 최소 indent 만큼 제거하고 출력 본문
Python에서 """로 선언된 여러 줄 문자열의 각 라인 최소 indent 만큼 제거하고 출력하는 방법이다.
각 라인의 맨 앞 indent 중 최소를 찾아 앞으로 붙여 정렬한다.
def adjust_indent(text: str) -> str:
lines = text.split('\n')
# 각 라인에서 공백 제외 부분의 길이를 찾아 최소 길이 계산
min_indent = min(len(line) - len(line.lstrip()) for line in lines if line.strip())
# 각 라인을 최소 indent만큼 제거하고 출력
adjusted_lines = [line[min_indent:] if line.strip() else line for line in lines]
return '\n'.join(adjusted_lines)
message ="""
AB
CD
EF
GH
"""
message = adjust_indent(message)
print(message)
'Python' 카테고리의 다른 글
Python pytube youtube 영상 다운로드 (0) | 2024.01.03 |
---|---|
Python JSON 문자열이 포함된 문자열, output string(stdout)을 JSON 객체로 파싱(parsing) (0) | 2024.01.01 |
Python 콜백 함수(Callback Function) 람다(Lambda) 매개변수 할당 (1) | 2023.12.29 |
Python 변수명 언더스코어(_) (0) | 2023.12.25 |
Python pymysql connection 재 연결(reconnect) (0) | 2023.12.21 |
Comments