강의

멘토링

로드맵

Inflearn Community Q&A

jmkim4458's profile image
jmkim4458

asked

Getting Started with Programming: Introduction to Python (Inflearn Original)

zfill, l or r just, 그리고 fomatter를 사용하지 않고 숫자 str 앞에 0을 추가할 수 있는 방법이 있나요?

Resolved

Written on

·

232

0

(아래 코드에서 s는 year = check_number1(year) 입니다. )

f"{a:02}"를 사용했을 때, 제 생년인 2001에서 1이라는 값을 얻은 다음 0이 앞에 들어가는 것이 아닌 10으로 출력이 됩니다. 이것을 어떻게 수정해야 01로 출력되고 이것 또한 사용하지 않고 01이라는 값을 출력할 수 있는 방법이 있나요?

 

def check_number1(s):
    try:
        s.isnumeric() == 1        
        a = str(int(s) % 100)
        return a.zfill(2)
    except:
        return '00'
python

Answer 1

0

niceman님의 프로필 이미지
niceman
Instructor

의도는 잘 모르겠으나, 

zfill 보다 으로 그냥 경우에 맞게 채우셔도 되요~

문자열 + 문자열 형태로만 만들어서요~~~

 

a = str(int(1) % 100)

print('0' + a)

jmkim4458's profile image
jmkim4458

asked

Ask a question