0% found this document useful (0 votes)
13 views

Slides 04 String Formatting

This document discusses different methods for building strings in Python, including using the + operator, format strings with % formatting codes, and f-strings. It provides examples of each method and explains format specifiers like %s, %d, %f that control how values are represented in strings. It also covers escape sequences that allow inserting special characters into strings.

Uploaded by

김은총
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Slides 04 String Formatting

This document discusses different methods for building strings in Python, including using the + operator, format strings with % formatting codes, and f-strings. It provides examples of each method and explains format specifiers like %s, %d, %f that control how values are represented in strings. It also covers escape sequences that allow inserting special characters into strings.

Uploaded by

김은총
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

String formatting

Format specifiers Building strings with +


f Strings Building strings with %
Escape sequences

String formatting
Python basics

Michael Burrell

August 28, 2023

Michael Burrell String formatting


String formatting
Format specifiers Building strings with +
f Strings Building strings with %
Escape sequences

Building strings with +

print(’You have ’ + str(num apples) + ’ apples and


’ + str(num oranges) + ’ oranges’)

Right now, if we want to build a string (e.g., to print it


out), we’re using the + operator
The + operator pastes two strings together

Michael Burrell String formatting


String formatting
Format specifiers Building strings with +
f Strings Building strings with %
Escape sequences

Building strings with +

print(’You have ’ + str(num apples) + ’ apples and


’ + str(num oranges) + ’ oranges’)

We have to explicitly convert to a string (using the str


function)
We have to break up the string into multiple parts
We don’t have a lot of control over how values are
converted into strings

Michael Burrell String formatting


String formatting
Format specifiers Building strings with +
f Strings Building strings with %
Escape sequences

Building strings with %

print(’You have %d apples and %d oranges’ %


(num apples, num oranges))

Another way to build strings is to have a format string


A string which optionally contains format specifiers
Format specifiers begin with a % sign and then have a
symbol (or a few symbols) after it. E.g., %d, %s, %f
After the format string is a modulus operator (%) and
then a tuple of values to fill in

Michael Burrell String formatting


String formatting
Format specifiers Building strings with +
f Strings Building strings with %
Escape sequences

Building strings with %

’You have %d apples and %d oranges’ % (


num apples, num oranges)

All of the format specifiers in the format string get


replaced by values
It works left-to-right (the first format specifier gets
replaced by the first value, and so on)
You must have the same number of format specifiers as
you do values

Michael Burrell String formatting


String formatting
Format specifiers Building strings with +
f Strings Building strings with %
Escape sequences

Building strings with %

’You have %d apples and %d oranges’ % (


num apples, num oranges)

All of the format specifiers in the format string get


replaced by values
It works left-to-right (the first format specifier gets
replaced by the first value, and so on)
You must have the same number of format specifiers as
you do values

Michael Burrell String formatting


String formatting
Format specifiers Building strings with +
f Strings Building strings with %
Escape sequences

Building strings with %

’You have %d apples and %d oranges’ % (


num apples, num oranges)

All of the format specifiers in the format string get


replaced by values
It works left-to-right (the first format specifier gets
replaced by the first value, and so on)
You must have the same number of format specifiers as
you do values

Michael Burrell String formatting


String formatting
Format specifiers
f Strings
Escape sequences

Format specifiers

There are many format specifiers that are defined in Python,


but the major ones are:
%s — print out a string (or automatically convert it
to a string if it’s not already a string). You can
put a number in front to provide alignment. E.g.,
’%20s’ will right-align a string to 20 characters,
even if the string is shorter than 20 characters
%d — print out an integer. You can put a number in
front to align it (e.g., %5d). You can put a
leading zero in front to require leading zeroes.
E.g., ’%04d’ % 19 will format as 0019

Michael Burrell String formatting


String formatting
Format specifiers
f Strings
Escape sequences

Format specifiers
There are many format specifiers that are defined in Python,
but the major ones are:
%f — print out a float. You can optionally provide
two leading numbers, one to give the total length
and one to give the number of digits after the
decimal point. E.g., %10.3f formats as 10
characters, with 3 of them being after the
decimal point. You can leave out the first number
(e.g., %.3f)
%e — like %f, but prints out using scientific notation
%g — like %f unless the number is very big or very
close to 0, in which case it uses %e instead

Michael Burrell String formatting


String formatting
Format specifiers
f Strings
Escape sequences

f Strings

Beginning in Python 3.6, “f strings” were added to the


language
Use a lot of the same format specifiers
A more convenient and easier-to-read way to include
values in strings

Michael Burrell String formatting


String formatting
Format specifiers
f Strings
Escape sequences

f Strings example

1 x = float(input(’Enter a number: ’))


2 print(f’{x:.2f} squared is {x**2:.2f}’)

The string literal is prefixed with the letter f


Curly-braces insert values, and any variable/expression
can be put in there
Optionally, we can include a : followed by a format
specifier

Michael Burrell String formatting


String formatting
Format specifiers
f Strings
Escape sequences

Escape sequences

Sometimes you’d like to put characters into a string that


normally are hard/impossible to put into a string
One example is trying to make a string that involves both
single quotes and double quotes

Michael Burrell String formatting


String formatting
Format specifiers
f Strings
Escape sequences

Escape sequences
In Python, you can put special characters into a string by
using a backslash (\) as an escape. Some popular escape
sequences:
\’ — make a literal ’ character
\” — make a literal ” character
\n — make a newline character, which puts the rest
of the text on the following line
\t — makes a horizontal tab character, which adds
horizontal space until it gets to a column that’s a
multiple of 8. It can be used for lining things up
\\ — for when you need a literal backslash character
in your string
Michael Burrell String formatting

You might also like