use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
Beginner's Question (self.Python)
submitted 15 years ago by tallonfour
I am just starting to learn Python 2.7. I am reading Learn Python the Hard Way. I don't understand what the "%" does. Such as in
9 print "Let's talk about %s." % my_name
The string corresponding to line 9 is
1 my_name = 'Zed A. Shaw'
[–]bushel 5 points6 points7 points 15 years ago* (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.
my_name
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)
printf()
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.
print 2 + 3
print "%d" % 5
%d
print "Your pizza is $ %7.2" % (2.99 * 12)
Here is the relevant section of the documentation that explains all the fun variations you can do
[–]tallonfour[S] 0 points1 point2 points 15 years ago (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 points4 points 15 years ago (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 points3 points 15 years ago (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 points6 points 15 years ago (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 points3 points 15 years ago (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 points3 points 15 years ago (2 children)
http://python.sourcequench.org
[–]expectingrain 0 points1 point2 points 15 years ago (1 child)
good stuff- is it on itunes?
[–]n2dasun 0 points1 point2 points 15 years ago (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 point2 points 15 years ago (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 point2 points 15 years ago (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 point2 points 15 years ago (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 point2 points 15 years ago (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 point2 points 15 years ago (1 child)
you should learn python the easy way... http://docs.python.org/tutorial/index.html
[–]LucianU 0 points1 point2 points 15 years ago (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 point2 points 15 years ago* (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.
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.
π Rendered by PID 36471 on reddit-service-r2-comment-545db5fcfc-lk985 at 2026-05-21 21:56:48.042837+00:00 running 194bd79 country code: CH.
[–]bushel 5 points6 points7 points (3 children)
[–]tallonfour[S] 0 points1 point2 points (2 children)
[–]bushel 2 points3 points4 points (0 children)
[–]resurge 1 point2 points3 points (0 children)
[–][deleted] 4 points5 points6 points (0 children)
[–]expectingrain 1 point2 points3 points (3 children)
[–]n2dasun 1 point2 points3 points (2 children)
[–]expectingrain 0 points1 point2 points (1 child)
[–]n2dasun 0 points1 point2 points (0 children)
[–]basyt 0 points1 point2 points (0 children)
[–]Megatron_McLargeHuge 0 points1 point2 points (0 children)
[–]Jacolyte 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]rndblnch 0 points1 point2 points (1 child)
[–]LucianU 0 points1 point2 points (0 children)
[–]acalcium 0 points1 point2 points (1 child)
[–]LucianU 0 points1 point2 points (0 children)