You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<pclass="Normal">Python has a powerful mechanism for formatting a string. This has now been implemented in GlowScript (for VPython, RapydScript, and JavaScript). Here is an example (in VPython):</p>
77
+
<pclass="Normal">The easiest way to format a string is to use what are called "f-strings", so-called because the string is preceded by "f" or, equivalently, "F":</p>
78
+
</div>
79
+
<div></div>
80
+
<div>
81
+
<pclass="program">V = 13.47<br>
82
+
units = 'liters'
83
+
<br/>
84
+
s = f"There are {V:.1f} {units}."<br/>
85
+
print(s) # "There are 13.5 liters." </p>
86
+
</div>
87
+
<div>
88
+
<pclass="Normal">The string begins with an initial "f" (or "F"). Quantities within braces are evaluated and converted to strings. The form {V:.1f} means "evaluate the variable V and display it in fixed format, with 1 digit after the decimal point. The value 13.47 is rounded to 13.5. If you specify {V:12.3f}, what will be displayed is " 13.470", with 6 spaces before the number, plus the 6 digits of "13.470" (3 after the decimal point), for a total of 12 characters.</p>
89
+
<pclass="Normal">A variable quantity such as V above can be a call to a function, whether your own function or a built-in function such as sqrt(). In fact, you can have entire expressions within braces, such as "10*V + sqrt(pi)".</p>
90
+
</div>
91
+
<div></div>
92
+
<div></div>
93
+
<div></div>
94
+
<div>
95
+
<pclass="Normal">There are many formats available in addtion to fixed format numbers: b for binary (base 2), o for octal (base 8), x for hexidecimal (base 16; 28 displays as 1c), d for decimal integer, e for exponential (e.g. 5.32e-7), c for character code (e.g. converts "b" to 97), % for percent (displays 0.34 as 34%). The E exponential format gives 5.32E-7 and the X hexidecimal format gives 1C. The g format (g for general) is particularly useful because it flips between fixed and exponential formats depending on the size of the number.</p>
96
+
</div>
97
+
<div></div>
98
+
<div>
99
+
<pclass="Normal">Currently the standard Python way to use f-strings in multiline situations does not work in GlowScript, so it is necessary to insert "\n" new-line characters: <spanclass="program">f"There are\n{V:12.3f} {units}."</span></p>
100
+
<div></div>
101
+
<div></div>
102
+
<div>
103
+
<div></div>
104
+
</div>
105
+
</div>
106
+
<div>
107
+
<h1class="Heading-1"><fontcolor="#0000a0">An older form of string formatting</font></h1>
108
+
<pclass="Normal">Here is an older form of string formatting):</p>
<pclass="Normal">The element "{}" indicates that the default method for representing an argument should be used. In this case "bucket" is displayed.</p>
126
+
<pclass="Normal">The element "{}" indicates that the default method for representing an argument should be used. In this case "liters" is displayed.</p>
0 commit comments