Skip to main content

Strings | Core Python 3.8

Strings 

Now what was all that "Hello, " + name + "!" stuff about? The first program in this chapter was simply

print("Hello, world!")

It is customary to begin with a program like this in programming tutorials. The problem is that I haven’t really explained how it works yet. You know the basics of the print statement (I’ll have more to say about that later), but what is "Hello, world!"? It’s called a string (as in “a string of characters”). Strings are found in almost every useful, real-world Python program and have many uses. Their main use is to represent bits of text, such as the exclamation “Hello, world!”
Single-Quoted Strings and Escaping Quotes Strings are values, just as numbers are:

>>> "Hello, world!" 

'Hello, world!'

There is one thing that may be a bit surprising about this example, though: when Python printed out our string, it used single quotes, whereas we used double quotes. What’s the difference? Actually, there is no difference.

>>> 'Hello, world!' 

'Hello, world!'

Here, we use single quotes, and the result is the same. So why allow both? Because in some cases it may be useful.

>>> "Let's go!" 

"Let's go!" 

>>> '"Hello, world!" she said' 

'"Hello, world!" she said'

In the preceding code, the first string contains a single quote (or an apostrophe, as we should perhaps call it in this context), and therefore we can’t use single quotes to enclose the string. If we did, the interpreter would complain (and rightly so).

>>> 'Let's go!' 

SyntaxError: invalid syntax

Here, the string is 'Let', and Python doesn’t quite know what to do with the following s (or the rest of the line, for that matter). 
    In the second string, we use double quotes as part of our sentence. Therefore, we have to use single quotes to enclose our string, for the same reasons as stated previously. Or, actually we don’t have to. It’s just convenient. An alternative is to use the backslash character (\) to escape the quotes in the string, like this:

>>> 'Let\'s go!' 

"Let's go!"

Python understands that the middle single quote is a character in the string and not the end of the string. (Even so, Python chooses to use double quotes when printing out the string.) The same works with double quotes, as you might expect.

>>> "\"Hello, world!\" she said" 

'"Hello, world!" she said'

Escaping quotes like this can be useful, and sometimes necessary. For example, what would you do without the backslash if your string contained both single and double quotes, as in the string 'Let\'s say "Hello, world!"'?

 ■ Note  tired of backslashes? as you will see later in this chapter, you can avoid most of them by using long strings and raw strings (which can be combined).
Concatenating Strings Just to keep whipping this slightly tortured example, let me show you another way of writing the same string:

>>> "Let's say " '"Hello, world!"' 

'Let\'s say "Hello, world!"'

I’ve simply written two strings, one after the other, and Python automatically concatenates them (makes them into one string). This mechanism isn’t used very often, but it can be useful at times. However, it works only when you actually write both strings at the same time, directly following one another.

>>> x = "Hello, " 

>>> y = "world!" 

>>> x y 

SyntaxError: invalid syntax

In other words, this is just a special way of writing strings, not a general method of concatenating them. How, then, do you concatenate strings? Just like you add numbers:

>>> "Hello, " + "world!" 

'Hello, world!' 

>>> x = "Hello, " 

>>> y = "world!" 

>>> x + y 

'Hello, world!'

String Representations, str and repr Throughout these examples, you have probably noticed that all the strings printed out by Python are still quoted. That’s because it prints out the value as it might be written in Python code, not how you would like it to look for the user. If you use print, however, the result is different.

>>> "Hello, world!" 

'Hello, world!' 

>>> print("Hello, world!") 

Hello, world!

The difference is even more obvious if we sneak in the special linefeed character code \n.

>>> "Hello,\nworld!" 

'Hello,\nworld!' 

>>> print("Hello,\nworld!") 

Hello, 

world!

Values are converted to strings through two different mechanisms. You can access both mechanisms yourself, by using the functions str and repr.9 With str, you convert a value into a string in some reasonable fashion that will probably be understood by a user, for example, converting any special character codes to the corresponding characters, where possible. If you use repr, however, you will generally get a representation of the value as a legal Python expression.

>>> print(repr("Hello,\nworld!")) 

'Hello,\nworld!' 

>>> print(str("Hello,\nworld!")) 

Hello, 

world!

Long Strings, Raw Strings, and bytes

There are some useful, slightly specialized ways of writing strings. For example, there’s a custom syntax for writing strings that include newlines (long strings) or backslashes (raw strings). In Python 2, there was also a separate syntax for writing strings with special symbols of different kinds, producing objects of the unicode type. The syntax still works but is now redundant, because all strings in Python 3 are Unicode strings. Instead, a new syntax has been introduced to specify a bytes object, roughly corresponding to the oldschool strings. As we shall see, these still play an important part in the handling of Unicode encodings.

Long Strings 

If you want to write a really long string, one that spans several lines, you can use triple quotes instead of ordinary quotes.

print('''This is a very long string.  It continues here. And it's not over yet.  "Hello, world!" Still here.''')

You can also use triple double quotes, """like this""". Note that because of the distinctive enclosing quotes, both single and double quotes are allowed inside, without being backslash-escaped.

 ■ Tip  Ordinary strings can also span several lines. If the last character on a line is a backslash, the line break itself is “escaped” and ignored. For example:
print("Hello, \ world!")
    Actually, str is a class, just like int. repr, however, is a function.
would print out Hello, world!. the same goes for expressions and statements in general.

>>> 1 + 2 + \    4 + 5 12 >>> print \    ('Hello, world') Hello, world

Raw Strings 

Raw strings aren’t too picky about backslashes, which can be very useful sometimes.10 In ordinary strings, the backslash has a special role: it escapes things, letting you put things into your string that you couldn’t normally write directly. For example, as we’ve seen, a newline is written \n and can be put into a string like this:

>>> print('Hello,\nworld!') 

Hello, world!

This is normally just dandy, but in some cases, it’s not what you want. What if you wanted the string to include a backslash followed by an n? You might want to put the DOS pathname C:\nowhere into a string.

>>> path = 'C:\nowhere' 

>>> path 'C:\nowhere'

This looks correct, until you print it and discover the flaw.

>>> print(path) 

C: owhere

It’s not exactly what we were after, is it? So what do we do? We can escape the backslash itself.

>>> print('C:\\nowhere') 

C:\nowhere

This is just fine. But for long paths, you wind up with a lot of backslashes.
path = 'C:\\Program Files\\fnord\\foo\\bar\\baz\\frozz\\bozz'
    Raw strings are useful in such cases. They don’t treat the backslash as a special character at all. Every character you put into a raw string stays the way you wrote it.
    Raw strings can be especially useful when writing regular expressions.

>>> print(r'C:\nowhere') 

C:\nowhere 

>>> print(r'C:\Program Files\fnord\foo\bar\baz\frozz\bozz') 

C:\Program Files\fnord\foo\bar\baz\frozz\bozz

As you can see, raw strings are prefixed with an r. It would seem that you can put anything inside a raw string, and that is almost true. Quotes must be escaped as usual, although that means you get a backslash in your final string, too.

>>> print(r'Let\'s go!') 

Let\'s go!

The one thing you can’t have in a raw string is a lone, final backslash. In other words, the last character in a raw string cannot be a backslash unless you escape it (and then the backslash you use to escape it will be part of the string, too). Given the previous example, that ought to be obvious. If the last character (before the final quote) is an unescaped backslash, Python won’t know whether or not to end the string.

>>> print(r"This is illegal\") 

SyntaxError: EOL while scanning string literal

Okay, so it’s reasonable, but what if you want the last character in your raw string to be a backslash? (Perhaps it’s the end of a DOS path, for example.) Well, I’ve given you a whole bag of tricks in this section that should help you solve that problem, but basically you need to put the backslash in a separate string. A simple way of doing that is the following:

>>> print(r'C:\Program Files\foo\bar' '\\') 

C:\Program Files\foo\bar\

Note that you can use both single and double quotes with raw strings. Even triple-quoted strings can be raw.

Comments

Popular posts from this blog

Execution the Program | Core Python 3.8

Saving and Executing Your Programs  The interactive interpreter is one of Python’s great strengths. It makes it possible to test solutions and to experiment with the language in real time. If you want to know how something works, just try it! However, everything you write in the interactive interpreter is lost when you quit. What you really want to do is write programs that both you and other people can run. In this section, you learn how to do just that.     First of all, you need a text editor, preferably one intended for programming. (If you use something like Microsoft Word, which I really don’t really recommend, be sure to save your code as plain text.) If you are already using IDLE, you’re in luck. With IDLE, you can simply create a new editor window with File › New File. Another window appears, without an interactive prompt. Whew! Start by entering the following: print("Hello, world!") Now select File › Save to save your program (which is, in fact, a pl...

Lists and Tuples | Core Python 3.8

This chapter introduces a new concept: data structures. A data structure is a collection of data elements (such as numbers or characters, or even other data structures) that is structured in some way, such as by numbering the elements. The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number—its position, or index. The first index is zero, the second index is one, and so forth. Some programming languages number their sequence elements starting with one, but the zeroindexing convention has a natural interpretation of an offset from the beginning of the sequence, with negative indexes wrapping around to the end. If you find the numbering a bit odd, I can assure you that you’ll most likely get used to it pretty fast.     This chapter begins with an overview of sequences and then covers some operations that are common to all sequences, including lists and tuples. These operations will also work with strings, which will be use...

Functions and Modules | Core Python 3.8

Functions  In the “Numbers and Expressions” section, I used the exponentiation operator (**) to calculate powers. The fact is that you can use a function instead, called pow. >>> 2 ** 3  8  >>> pow(2, 3)  8 A function is like a little program that you can use to perform a specific action. Python has a lot of functions that can do many wonderful things. In fact, you can make your own functions, too (more about that later); therefore, we often refer to standard functions such as pow as built-in functions.     Using a function as I did in the preceding example is called calling the function. You supply it with arguments (in this case, 2 and 3), and it returns a value to you. Because it returns a value, a function call is simply another type of expression, like the arithmetic expressions discussed earlier in this chapter.6 In fact, you can combine function calls and operators to create more complicated expressions (like I did...

Exceptions | Core Python 3.8

When writing computer programs, it is usually possible to discern between a normal course of events and something that’s exceptional (out of the ordinary). Such exceptional events might be errors (such as trying to divide a number by zero) or simply something you might not expect to happen very often. To handle such exceptional events, you might use conditionals everywhere the events might occur (for example, have your program check whether the denominator is zero for every division). However, this would not only be inefficient and inflexible but would also make the programs illegible. You might be tempted to ignore these exceptional events and just hope they won’t occur, but Python offers an exception-handling mechanism as a powerful alternative.     In this tutorial, you’ll learn how to create and raise your own exceptions, as well as how to handle exceptions in various ways. What Is an Exception?  To represent exceptional conditions, Python uses exception obj...