티스토리 뷰

How to change a string to a variable

스트링을 변수로 바꾸는 방법

 

값의 위치(순서)에 상관없이 변수에 값을 대입할 수 있는 방법으로 사용하는 것이 좋음.

변수의 순서를 모두 기억해서 순서대로 나열해야 하는 방법은 비효율적이기 때문.

 

 

String 상태 그대로 출력됨

def string_variable_test(country, food):
	return "I'm living in country and my favorite traditional food is food"
    
sv_test = string_variable_test("Korea", "lemonade")
print(sv_test)

>>> I'm living in country and my favorite traditional food is food

 

String을 Variable로 바꾸기 {}

변수에 값이 대입되어 출력됨

# --> The way from a String to a Variable.
# --> Using f (format).

f"I'm living in {country} and my favorite traditional food is {food}"

# --> I'm living in Korea and my favorite traditional food is lemonade

 

순서에 관계없이 변수에 값 대입되게 출력하기

country="대입할값"

# --> The way to position keyword without order.
# --> Using keyword argument.

def string_variable_test(country, food):
	return f"I'm living in {country} and my favorite traditional food is {food}"

sv_test = string_variable_test(food="lemonade", country="Korea")
print(sv_test)

>>> I'm living in Korea and my favorite traditional food is lemonade

댓글
최근에 올라온 글
페이지 이동 안내

보던 글 목록 : 브라우저 뒤로 가기 메인 화면 : 좌측 상단 아이콘
🍍 The GOAL: AI expert에 가까워지는 중

🍍 I am becoming AI expert who can develop cool things by coding.