Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use Python # f-string exercises # x, y, z variables for the exercise x = 27 y = 3.14159 z = 'pi' # build f-strings

Use Python

image text in transcribed

image text in transcribed

image text in transcribed

# f-string exercises

# x, y, z variables for the exercise

x = 27

y = 3.14159

z = 'pi'

# build f-strings to each specification below

# 10 different exercises of increasing complexity

''' #1

Create a string variable s that uses

variable x to create this string ...

"27 is a whole number"

'''

# edit the assignment statement below

s = f''

print(f"#1 ... {s}")

''' #2

Create a string variable s that uses variable x

and the type() function to create this string ...

"27 is "

'''

# edit the assignment statement below

s = f''

print(f"#2 ... {s}")

''' #3

Create a string variable q that uses

variable y to create this string ...

"pi to 6 digits is 3.14159"

'''

# edit the assignment statement below

q = f''

print(f"#3 ... {q}")

''' #4

Create a string variable q that uses

variable y to create this string ...

"pi to 3 digits is 3.14"

'''

# edit the assignment statement below

q = f''

print(f"#4 ... {q}")

''' #5

Create a string variable r that uses

variables y and z to create this string ...

"pi to 4 digits is 3.14"

'''

# edit the assignment statement below

r = f''

print(f"#5 ... {r}")

''' #6

Create a string variable r that uses variables x,

y, and z to create this output ...

" 27 3.14159 pi "

Each variable is centered across 10 spaces

'''

# edit the assignment statement below

r = f''

print(f"#6 ... {r}")

''' #7

Create a string variable r that uses variables x,

y, and z to create this table ...

" x y z

27 3.14159 pi "

Each item is centered across 10 spaces.

Use the newline escape sequence as needed.

'''

# edit the assignment statement below

r = f''

print(f"#7 ... {r}")

''' #8

Create a string variable q that uses variables x and

y as US currency (two decimal places, preceded

by a dollar sign, left justified across 10 spaces ...

"$27.00 $3.14 "

'''

# edit the assignment statement below

q = f''

print(f"#8 ... {q}")

''' #9

Create a string variable q that uses variables x and

y as US currency (two decimal places, preceded

by a dollar sign, center justified across 10 spaces ...

" $27.00 $3.14 "

'''

# edit the assignment statement below

q = f''

print(f"#9 ... {q}")

''' #10

Create a string variable s that uses variables x and

y as US currency (two decimal places, with room for

4 digits left of the decimal point preceded

by a dollar sign and center justified across 10 spaces ...

" $ 27.00 $ 3.14 "

'''

# edit the assignment statement below

s = f''

print(f"#10 ... {s}")

Formatted String Literals f-string exretises Formatted String Literals When we are introdsced to Psthon, the fisst stalenrent we wite is pererally a call to the pintul of precision to the rivit (3 charscters). This leases 3 cibits to the left of the deciras peint. If result is tunntion. As we progenss as pragrsmmers, we use the print 0 turction to interset with the iser arturber lareer thisi 999.99, dists will te adved to the le't and the widti you specified wil not be througt the Pythan shell and weport the ressults of aur computstione. At some peirs in cur respersed tar there will still be two dights ta the right at the decimal point. Hene wo say the procisian is dssollepment, we get to the paint wters we want to gererate output as taaksat of numbers. Fiather, those numbers often have mearinc in the form of physical units or currency that mey require then to be formatted in a particular way. If vou are lookire to print columrs of fixed grecisian numbers, i's inportant to have an eytimate of how dipits siter the decimsl point that are not lelsfel in commuricatirg the reeslt. It s often desirable to print rows of numbers in celurins thes line up neatly. There are seseral met iods that you con use to for example, o-f. reaylt-(reoult:0.2f)" will formet the reoult wriatle so s fost with two dirits of presision usirf exsetly the rumber of dipits nsesessary and no more. Nots that you can either liturals" "semintienes called I fostrings," specity zerov cifits or leave the ficld tlank .. eitther was vil gine the same result. The specal case af curreney. It you are ty ving to format manctary walues, there soc cortain add ditionsl string ionke like this: f. This generates a null icminty string ... nat wery ceseiting. You can assign an t string to a wsriable and put text tecwesen the quatcei ind it wal just create a atring 2= f' thla is telow. Scueral inwatec nossing tatrings inside ot t stringi. The bess focmasting for moremary wausi in ger comiting, thoughi, aten yous start to insert t string cexpressiens the same column. Iry these out vcurself. All of these examples print out the waines in this list: just contains s wariatle name or Python expression hor example: s=t 'resylt= (result) 'just crestee a stine containine the text 'result=" followed ty the value of the varibble result. Ihis would hove the same resul as this statement: x= rexulte' 4xtr(xmxult). veu cen ajecify that the result is in 320 -character wice column by sddine: 10 after the veriable a aF'xeaylt-[ [ eoult:10] '. The column wicth is not required to be a contant ... it could actualhy be a propram vuriable. Supoose yeu wart to vary the column with with s variatle callsd wieth ... veu coulc replase : 10 with: (w1dth) like this: column width spectier... The caze gf fleat variables. When you simply print the value of a flcat varisble, vou con't always know represersed ty the flost wariatle. In the case of expressing amounts of money iie dollarsi veu really only care about two difits to the rigtt of the decimal point ioften called "precisicn'). tstrine expressicrs pive vau a voy to cantrol the rumber af dipits to the rith of the decinal place ard Formatted String Literals Methout 1 allows for Brester Ilexibility. All Forinst parameters could be prorided as variables. Method 2 is quick but not as flexible 5pace between columns is hard-coded. Formatted String Literals f-string exercises THE EXERCISES

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

4th Edition

0615231047, 978-0615231044

More Books

Students also viewed these Databases questions

Question

How will the members be held accountable?

Answered: 1 week ago

Question

a. How will the leader be selected?

Answered: 1 week ago