Python 3.1: String Formatting

In this conclusion to a three-part series on Python 3.1, you’ll learn how to format strings and put them together in different ways. We’ll also review what we covered in the series as a whole, which includes your first Python program. This article is excerpted from the book Beginning Python: Using Python 2.6 and Python 3.1,, written by James Payne, Developer Shed Editor-in-Chief (Wrox, 2010; ISBN: 0470414634).

If you like what you’re reading here, you can find it at Wrox or purchase the entire book at Amazon

Putting Strings Together in Different Ways

Another way to specify strings is to use a format specifier. It works by putting in a special sequence of characters that Python will interpret as a placeholder for a value that will be provided by you. This may initially seem like it’s too complex to be useful, but format specifiers also enable you to control what the displayed information looks like, as well as a number of other useful tricks. 

Try It Out: Using a Format Specifier to Populate a String 

In the simplest case, you can do the same thing with your friend, John Q.:

> > > “John Q. %s” % (“Public”)

‘John Q. Public’

How It Works

The %s is known as a format specifier, specifically for strings. As the discussion on data types continues throughout this book, you take a look at several more, each specific to its given data type. Every specifier acts as a placeholder for that type in the string; and after the string, the % sign outside of the string indicates that after it, all of the values to be inserted into the format specifier will be presented there to be used in the string.

You may notice the parentheses. This tells the string that it should expect to see a sequence that contains the values to be used by the string to populate its format specifiers.

A simpler way to think of it is to imagine that the %s is a storage bin that holds the value in the parentheses. If you want to do more than one value, you would simply add another format specifier, in this manner:

> > > ”John %s%s” % (“Every” , “Man”)

John Everyman

These sequences are an integral part of programming in Python, and as such, they are covered in greater detail later in this book. For now, just know that every format specification in a string has to have an element that matches it in the sequence that is provided to it. Each item in the sequence are strings that must be separated by commas.

So why do they call it a format specifier if you store data in it? The reason is that it has multiple functions; being a container is only one of them. The following example shows you how to not only store data with the format specifier, but specify how that data will be displayed as well.

{mospagebreak title=Try It Out: More String Formatting} 

In this example, you tell the format specifier how many characters to expect. Try the following code and watch what happens:

> > > “%s %s %10s” % (“John” , “Every”, “Man”)

‘John Every Man’

> > > “%-5s %s %10s” % (“John” , “Every”, “Man”)

John Every Man

How It Works

In the first line of code, the word Man appears far away from the other words; this is because in your last format specifier, you added a 10, so it is expecting a string with ten characters. When it does not find ten (it only finds three . . . M – a – n) it pads space in between with seven spaces.

In the second line of code you entered, you will notice that the word Every is spaced differently. This occurs for the same reason as before — only this time, it occurred to the left, instead of the right.

Whenever you right a negative ( – ) in your format specifier, the format occurs to the left of the word. If there is just a number with no negative, it occurs to the right.

{mospagebreak title=Summary}

In this chapter you learned how to install Python, and how to work with the Python GUI (IDLE), which is a program written in Python for the express purpose of editing Python programs. In addition to editing files, this “ shell ” allows you to experiment with simple programming statements in the Python language.

Among the things you learned to do within the shell are the basics of handling strings, including string concatenation, as well as how to format strings with format specifiers, and even storing strings within that same %s format specifier. In addition, you learned to work with multiple styles of quotes, including the single, double, and triple, and found out what the n newline escape character was for.

Finally, you learned your very first function, print() , and wrote your first program, the Hello World standby, which is a time – honored tradition among programmers; it’s similar to learning “ Smoke on the Water ” if you play guitar — it’s the first thing you’ll ever learn.

The key things to take away from this chapter are: 

  • Programming is consistency. All programs are created with a specific use in mind, and your user will expect the program not only to live up to that usage, but to work in exactly the same manner each and every time. If the user clicks a button and a print dialog box pops up, this button should always work in this manner.

  • Programming is control. As a programmer, you control the actions your application can and cannot take. Even aspects of the program that seem random to the casual observer are, in fact, controlled by the parameters that you create.

  • Programming copes with changes. Through repeated tests, you can ensure that your program responds appropriately to the user, even when they ask the program to do something you did not develop it to do.

  • Strings are a data type, or simply put, a category of data. These strings allow you to interact with the user in a plethora of ways, such as printing text to the window, accepting text from the user, and so forth. A string can consist of any letter, number, or special character.

  • The print() function allows you to print text to the user’s screen. It follows the syntax: print( “ Here is some text ” ).


1. In the Python shell, type the string, “ Rock a by baby,nton the tree top,twhen the wind blowsnttt the cradle will drop. ” Feel free to experiment with the number of n and t escape sequences to see how this affects what gets displayed on your screen. You can even try changing their placement. What do you think you are likely to see?

2. In the Python shell, use the same string indicated in Exercise 1, but this time, display it using the print() function. Once more, try differing the number of n and t escape sequences. How do you think it will differ?


[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye