https://medium.com/@charliesharding/learning-python-no-template-literals-da7cbd77e3ba
Coming from Javascript, python is a fairly approachable language. I’m going to try to keep up these posts as a reference for others learning python as well. One of the first things that I’ve encountered is the absence of template literals in the language.
In Javascript, one can type:
let age = 27;
console.log(`my age is ${age}!`);
And the output will be 'my age is 27!'
. This is useful when you have a lot of embedded variables in a string so that you don’t have to keep concatenating with ending quotes and plus signs.
'lol ' + adversary.name + ' you suk at ' + adversary.favoriteThing + '!!!!111!11!'
I found an active proposal for this feature but it was posted two years ago. https://www.python.org/dev/peps/pep-0498/
Turns out they totally do exist, just under a different name and syntax.
x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)
The %s and % (varnames) is the way of inserting the values of those variables into the string. %s is used for strings whereas %d is used for numbers.
As pointed out in the comments by Phil Owens — a better functionality actually has been added in to python 3.6
“As of 3.6 you can use the syntax:
date = '22nd'txt = f"Today is March {date}."
“
Thanks Phil!
'C Lang > Python Program Diary' 카테고리의 다른 글
generator를 사용해야하는 이유 : 메모리 절약(하지만 속도는 리스트보다 느림) (0) | 2019.11.06 |
---|---|
iterator객체, for문 직접 만들어보기 (0) | 2019.11.06 |
itertools : 곱집합, 조합, 순열 등 계산을 간단히 해주는 파이썬 내장 모듈 (0) | 2019.10.31 |
python에서 변수나 리턴의 타입을 지정 : typing, NewType, generics, (0) | 2019.10.21 |
파이썬의 시간대에 대해 알아보기, naive datetime, aware datetime (0) | 2019.10.09 |