본문 바로가기
파이썬

파이썬 Ruby의 문자열 보간에 해당하는 Python이 있습니까?

by º기록 2020. 10. 18.
반응형

Ruby 예 :

name = "Spongebob Squarepants"
puts "Who lives in a Pineapple under the sea? \n#{name}."

성공적인 파이썬 문자열 연결은 겉으로보기에 장황 해 보입니다.

 

해결 방법

 


name = "Spongebob Squarepants"
print(f"Who lives in a Pineapple under the sea? {name}.")

3.6 이전에 가장 가까운 것은

name = "Spongebob Squarepants"
print("Who lives in a Pineapple under the sea? %(name)s." % locals())


최신 Python 버전의 .format () 메서드를 사용하는 동일한 코드는 다음과 같습니다.

name = "Spongebob Squarepants"
print("Who lives in a Pineapple under the sea? {name!s}.".format(**locals()))


tmpl = string.Template("Who lives in a Pineapple under the sea? $name.")
print(tmpl.substitute(name="Spongebob Squarepants"))

 

참조 페이지 https://stackoverflow.com/questions/4450592

 

 

반응형

댓글