print() 함수에서, 쉽게 여러 값들을 출력하는 방법은 콤마(,)를 사용해 출력값들을 구분해주는 방법이다.
한 칸 공백이 디폴트로 설정되어있다.
print('A1', 'B2')
A1 B2
sep 파라미터는 구분자를 지정할 수 있게 해준다.
이 경우는 구분자로 콤마(,)를 사용했다.
print('A1', 'B2', sep=',')
A1,B2
end 파라미터를 사용하면 출력값 뒤에 지정한 값을 넣어줄 수 있다.
print() 함수가 항상 줄바꿈을 하기에 end 파라미터를 공백으로 지정하면 붙혀서 값들을 출력할 수 있다.
print('mincheol', end=' ')
print('kim')
mincheol kim
print() 함수에 f-string 을 사용하면 효과적으로 여러값들을 출력해 낼 수 있다.
f - string 은 문자열 내에 특정 문자열을 치환할 때 사용한다.
기본형태
name = "mincheol"
age = 100
print(f"Hello, {name}. You are {score}.")
'Hello, mincheol. Your score is 100.'
f 를 대문자 F 로 써도 가능
print(F"Hello, {name}. You are {score}.")
'Hello, mincheol. Your score is 100.'
숫자 연산도 가능
print(f"{2 * 3}")
'6'
함수도 불러올 수 있음
def to_lowercase(input):
return input.lower()
name = "Mincheol Kim"
print(f"{to_lowercase(name)}.")
'mincheol kim.'
메서도도 가능
print(f"{name.lower()}.")
'mincheol kim.'
클래스와 함께 쓰는 f-string
class Car:
def __init__(self,color,mileage):
self.color = color
self.mileage = mileage
blue_car = Car("blue", 200)
red_car = Car(color="red",mileage=300)
for car in (blue_car,red_car):
print(f"The {car.color} car has {car.mileage} miles")
The blue car has 200 miles
The red car has 300 miles