Skip to Content
The Python Book
book

The Python Book

by Rob Mastrodomenico
January 2022
Beginner to intermediate
272 pages
6h 51m
English
Wiley
Content preview from The Python Book

12Strings

Python is good at many things, I may be biased but I believe it's true, however one thing that its particularly good at is dealing with strings and string manipulation. This chapter will expand what we already know about strings showing how powerful Python is at dealing with them. So let's begin with looking at what a string is:

image

In the above example it doesn't matter which way you create the string and it is all the same but there are a few examples where you need to create a string differently. This primarily revolves around the use of single and double quotes. Now, if you put a single quote in a string separated by single quotes you get the following:

image

To include a single quote in a string separated by single quotes you need to use an escape sequence. An escape sequence is basically the character with a backslash before it, so if we write the previous string with an escape sequence it looks like the following:

image

You might say why use single quotes around a string with single quotes and you would be right, we could write the previous example as follows with double quotes:

We can get away with not using escape sequences if we use triple quotes. So we can rewrite ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

The Quick Python Book, Third Edition

The Quick Python Book, Third Edition

Naomi Ceder, David Fugate
Serious Python

Serious Python

Julien Danjou

Publisher Resources

ISBN: 9781119573319Purchase Link