본문 바로가기

포맷팅 {}안의 값은 숫자로 표현format 인자의 인덱스로 사용>>> print("{0} is {1}".format("apple", "red"))apple is red>>> print("{0} is {1} or {2}".format("apple", "red", "green"))apple is red or green format의 인자로 키와 값을 지정해 사용 가능>>> print("{item} is {color}".format(item="apple", color="red"))apple is red 사전을 입력으로 받는 경우>>> dic = {"item":"apple", "color":"red"}>>> print("{0[item]} is {0[color]}".format(dic))apple is red0[ite.. 더보기
출력 인자가 여러 개 들어가면 공백으로 구분해서 출력+ 연산자를 이용하면 공백이 없음>>> print(x, 'test')0.2 test>>> print(a + 'this is test')'hello\n'this is test 구분자(sep), 끝라인(end), 출력(file)을 지정할 수 있음>>> import sys>>> print("welcome to", "python", sep="~", end="!", file=sys.stderr) >> f = open('test.txt', 'w')>>> print('file write', file = f)>>> f.close() 화면에 출력할 때 중을 맞추거나 정렬할 때string 객체에서 제공되는 함수를 이용print() 함수에서 제공하는 format 이용>>> fo.. 더보기
repr(), str(), ascii() str은 실제 객체의 값과 다를 수가 있습니다. eval(repr(obj))는 실제 obj와 동일한 값을 생성할 수 있어야 합니다. 하지만 eval(str(obj))는 실제 obj와 동일한 값이 아니거나, 오류를 내는 경우가 있을 수 있습니다.eval()는 string형식으로 받은 문자열을 그대로 실행해준다.ascii()은 아스키에 해당하는 문자열에 대해서 정확히 동일한 값을 반환한다.다만 아스키 이외의 값에 대해서는 백슬래시를 사용한 유니코드값을 반환한다.[출처] Python, str(), repr(), ascii(),eval()|작성자 Youngjae[출처] Python, str(), repr(), ascii(),eval()|작성자 Youngjae>>> f = 0.3 >>> f 0.2999999999.. 더보기