Computer Science/+α

[python] 출력, 소수점 정리

hyunjin 2023. 11. 10. 00:54

헷갈리는 것 위주로 정리하였다.

 

format 형태

x, y = 3, "hi"
print("x is {0} and y is {1}" .format(x, y))
print("x is {} and y is {}" .format(x, y))
print("x is {new_x} and y is {new_y}" .format(new_x=x, new_y=y))

f string 형태

first_name = "John"

print(f"Hello, {first_name}!")

 

 

소수점

"{ : .2f }.".format(실수 혹은 변수) : 소수점 3번째 자리에서 반올림을 해서 2자리 까지 출력

f'{변수:.2f}':  소수점 3번째 자리에서 반올림을 해서 2자리 까지 출력

 

※소수점 처리

round(실수 혹은 변수, 2): 소수점 3번째 자리에서 반올림을 해서 2자리 까지 출력

올림: math.ceil()

내림: math.floor()