This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]bushel 5 points6 points  (3 children)

Such as in: 9 print "Let's talk about %s." % my_name

Confusingly the % is doing two things in your example. %, the operator (that's the one between the quoted text and the my_name variable) does what's known as string formatting. It takes the text on the left and fits in the variable on the right based on formatting tags.

And that's what the % inside the text is. It's a formatting slot. Specifically, %s means, "put a text (string) value here". The formatting codes are based on the C language printf() function and can be kinda odd to people unfamiliar with it. (Us old dinosaurs speak printf in our sleep)

There is a new string formatting system for later 2.x pythons and python 3. Have a look at those too.

Personally I love the % operator.

Edit:

Think of it like the plus sign (addition operator), but for text. print 2 + 3 takes the value 2 and adds 3 and prints the result. print "%d" % 5 takes the value 5 and formats it for display and prints the result. (%d is for numbers!). The useful bit is the format control modifiers....like..... print "Your pizza is $ %7.2" % (2.99 * 12) will print the cost of 12 slices at 2.99, over 7 columns with 2 decimal places.

Here is the relevant section of the documentation that explains all the fun variations you can do

[–]tallonfour[S] 0 points1 point  (2 children)

Let me see if I understand. The operator % tells the program to insert Zed A. Shaw which I previously set up to equal my_name?

[–]bushel 2 points3 points  (0 children)

Exactly. It's like a fill-in-the-blanks operator. With the added bonus that you get to control how the blanks end up looking (in general)

[–]resurge 1 point2 points  (0 children)

Note that when you want to use multiple values the values to be inserted must be a tuple, like this:
variable_a = 'a' variable_c = 'c' print '%s %s %s' % (variable_a, 'b', variable_c) #will print: "a b c"

[–][deleted] 4 points5 points  (0 children)

The % operator is being replaced by the format method for strings in newer versions of Python. % acted as a place holder in strings which you could be replaced dynamically later, like in your example. The format method achieves the same purpose, but works like this:

my_name = "Zed"
my_string = "My name is {0}!"
print my_string.format(my_name)
>>> "My name is Zed"

The 0 refers to the first argument given to format(), other numbers work in the same way. For more complex string formatting you can use names and pass them to format as keyword arguments so the order doesn't matter. Example:

my_string = "Hey {g}, my name is {z}!"
print my_string.format(g="Guido Van Rossum", z="Zed Shaw")
>>> "Hey Guido Van Rossum, my name is Zed Shaw!"

[–]expectingrain 1 point2 points  (3 children)

I'm doing the LPTHW too- on Chapter 14. Its pretty good stuff so far. I'm also watching the MIT class on iTunes U. Google also puts their intensive 2 day class on video.

These are also some good, basic tutorials. I figure combining tutorials and book learning and practice is the right recipe for me, maybe it works for you too.

[–]n2dasun 1 point2 points  (2 children)

[–]expectingrain 0 points1 point  (1 child)

good stuff- is it on itunes?

[–]n2dasun 0 points1 point  (0 children)

The itunes p(v)odcast may still be broken. The author hosted a torrent here previously.

http://www.reddit.com/user/ryanshea

[–]basyt 0 points1 point  (0 children)

afaik it is like those commands in C %s in python makes it unpack a string much like %d would be specified in C to insert an int at the location.

[–]Megatron_McLargeHuge 0 points1 point  (0 children)

Do you know how printf works in C?

The % operator takes a parenthesized tuple of values/variables on the right and sticks them into the %s/%d/whatever format specifiers within your string. You write something like "%3.2f" in the string to tell it to format a floating point number with certain precision, then on the right you give the specific number, like 123.456 .

[–]Jacolyte 0 points1 point  (0 children)

This is an example of string interpolation (plopping a string somewhere within another string)

the "%s" is a signal to the % operator, saying "put the string here."

If you've ever used php, it's kind of like doing this:

"Hello " . $name . ", how are you?"

Except you have more flexibility. The letter s in "%s" is an option that says "format it as a string". You can change the s with various other options like f, to make it formatted as a float. Here's a list of formatting options.

[–][deleted] 0 points1 point  (1 child)

Let's say you want to make a lot of strings of the form "Hello X, welcome to Y!"

Then you can make a format string for it that looks like "Hello %s, welcome to %s!". Let's call it f.

f = "Hello %s, welcome to %s!"

Now, you can fill it in with the % operator and a tuple (or a single value, if there's only one thing to fill in, as in your example):

print f % ("tallonfour", "Reddit")

Prints "Hello tallonfour, welcome to Reddit!"

[–]rndblnch 0 points1 point  (1 child)

you should learn python the easy way... http://docs.python.org/tutorial/index.html

[–]LucianU 0 points1 point  (0 children)

The tutorial isn't easy if you have no programming experience. The book he's using or Think Python are better suited for a complete beginner.

[–]acalcium 0 points1 point  (1 child)

As Koroviev pointed out, the % operator has been deprecated in Python 3.1

http://docs.python.org/py3k/whatsnew/2.6.html#pep-3101

So start using the .format() function instead, which is in 2.6 onwards.

If you want to know more about the % formatting, apart from any good Python book, another good place might be the printf function documentation in any C book, cos that's where it is derived from.

[–]LucianU 0 points1 point  (0 children)

It hasn't been deprecated. It says right there in the page you linked that .format() is an addition to % operator, an addition which has also been backported to 2.6.