Print f ua dab tsi hauv Python?
Print f ua dab tsi hauv Python?

Video: Print f ua dab tsi hauv Python?

Video: Print f ua dab tsi hauv Python?
Video: Python! Formatted String Literals (f-strings) 2024, Plaub Hlis Ntuj
Anonim

F -Txoj hlua muab txoj hauv kev yooj yim thiab yooj yim rau embed python cov kab lus nyob rau hauv txoj hlua literals rau formatting. luam tawm ( f "{val}rau{val} yog ib lub portal rau {val}.") luam tawm ( f "Nyob zoo, Kuv lub npe yog {npe} thiab kuv yog {hnub nyoog} xyoo.")

Cov lus nug tseem yog, dab tsi yog kev siv F hauv Python?

F -Txoj hlua muab ib txoj hauv kev rau embed kab lus nyob rau hauv txoj hlua literals, siv ib tug tsawg syntax. Nws yuav tsum tau muab sau tseg tias ib f -Txoj hlua yog qhov qhia tau ntsuas ntawm lub sijhawm khiav, tsis yog tus nqi tas li. Hauv Python source code, ib f -Txoj hlua yog ib txoj hlua literal, prefixed nrog ' f ', uas muaj cov kab lus hauv cov hlua khi.

Ib yam li ntawd, txoj hlua F yog dab tsi? Kuj hu ua “formatted txoj hlua literals,” f - hlua yog txoj hlua literals uas muaj ib f thaum pib thiab curly braces muaj cov kab lus uas yuav raug hloov nrog lawv cov txiaj ntsig. Cov kab lus raug ntsuas ntawm lub sijhawm ua haujlwm thiab tom qab ntawd formatted siv _format_ raws tu qauv.

Tsuas yog li ntawd, %s thiab %D yog dab tsi hauv Python?

% s yog siv los ua qhov chaw rau txoj hlua qhov tseem ceeb uas koj xav txhaj rau hauv txoj hlua formatted. % d yog siv los ua qhov chaw rau cov lej lossis lej. Piv txwv li (rau python 3) luam tawm ('% s yog % d xyoo laus' % ('Joe', 42)) Yuav tso Joe yog 42 xyoo.

%s txhais li cas hauv Python?

% s yog hom specifier. Lub luag haujlwm ntawm% s yog nws hais qhov python tus neeg txhais lus txog hom ntawv twg nws yuav luam tawm, ntawm lub console. Txoj hlua yog cov format nyob rau hauv cov ntaub ntawv no. Yog li cov syntax mus ib yam dab tsi zoo li no.

Pom zoo: