String Formatting

String Formatting

Formatting Strings

? A simple way to format is the use of f-strings ? A string preceded with an f-character ? Can put variables into braces { } ? Example

f'This is pi: {math.pi}'

Formatting Strings

? We really need to learn how to format strings ? Python has made several attempts before settling on an ef cient syntax. ? You can nd information on the previous solutions on the net. ? Use the format function ? Distinguish between the blueprint ? and the string to be formatte ? Result is the formatted string.

d

if if

Formatting Strings

? Blueprint string ? Uses {} to denote places for variables ? Simple example ? "{} {}".format('one', `two')

Blueprint

Calling format

String to be formatted

? Result

`one two'

Formatting Strings

? Inside the brackets, we can put indices to select variables ? 0 means rst variable, 1 second, ... ? Can reuse variables

if

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download