Home arrow Python arrow Page 3 - Python 3.1: String Formatting

Summary - Python

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

  1. Python 3.1: String Formatting
  2. Try It Out: More String Formatting
  3. Summary
By: James Payne
Rating: starstarstarstarstar / 3
May 12, 2010

print this article



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,\n\ton the tree top,\t\when the wind blows\n\t\t\t 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?


>>> More Python Articles          >>> More By James Payne

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: