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 ...

Get The Python Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.