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

fi

?

fi

?

Formatting Strings

?

Blueprint string

?

Uses {} to denote places for variables

?

Simple example

?

"{} {}".format('one', ¡®two')

Blueprint

?

Result

Calling

format

¡®one two¡¯

String to be

formatted

Formatting Strings

Inside the brackets, we can put indices to select

variables

?

0 means rst variable, 1 second, ¡­

?

Can reuse variables

fi

?

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

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

Google Online Preview   Download