#python #anaconda #jupyter-notebook #visual-code #html #css #javascript #http

제목str 클래스의 method 중 format에 대해 궁금한 점이 있습니다. 2018-10-17 15:26
작성자

다음과 같은 형식은 정상적으로 작동합니다.


a=input('무엇을 넣겠습니까?')

"i ate {number}".format(number=a)


하지만 format의 문법 중 문자열 정렬에서, 반복할 문자열과 공간을 임의로 정해두고 싶어서 

###

temp="Dr.Sungwon"

temp2="{0:{whatinput}^{howmuch}}"

a = input('넣고 싶은 문자')

b = int(input('넣고 싶은 갯수'))

print(temp2.format(whatinput=a,howmuch=b))

###

과 같은 형식으로 코드를 짰으나, 정상적으로 작동하지 않았습니다. (tuple index out of range)

하지만 다음과 같은 코드는 정상적으로 작동하였습니다.

###

temp="Dr.Sungwon"

temp2="{0:{0}^{1}}" 

a = input('넣고 싶은 문자')

b = input('넣고 싶은 갯수')

print(temp2.format(a,b))

###


제 나름대로 맨 처음 코드를 응용해서 두 번쨰 코드를 짰지만 실행되지 않는 이유가 궁금합니다..

댓글