Format Characters

This is the place for queries that don't fit in any of the other categories.

Format Characters

Postby medavidcook » Wed Feb 13, 2013 10:30 pm

Hello

I am trying to get my head around something

Can anyone tell me what each of these stand for

%d
%r
%s

are there any more

thanks
medavidcook
 
Posts: 9
Joined: Mon Feb 11, 2013 11:20 am
Location: Cardiff, Wales

Re: Format Characters

Postby Yoriz » Wed Feb 13, 2013 10:45 pm

Most probably string formating, you can read about it here
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 724
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Format Characters

Postby khadis » Thu Feb 14, 2013 3:32 am

medavidcook wrote:Hello

I am trying to get my head around something

Can anyone tell me what each of these stand for

%d
%r
%s

are there any more

thanks


Hello,
Actually I am a newbie, so my explanation could be not 100% right :D
I used to interpret them as (%d=digit: "to read", to show, or get a result from an input that contains number, %s=string: "to read", to show, or get a result from an input that contains words, and %r is actually "to show" something as written in the script).
example #1:
Code: Select all
name = raw_input("What is your name? ")
print "Hi %s. How are you?", (name)

The %s will return into any word you enter/type after "What is your name?" question.

example #2:
Code: Select all
age= input("How old are you? ")
print "Oh, %d. You are so young.", (age)

The %d will return into any number you enter/type after "How old are you?"

Sorry for confusing explanation :D
khadis
 
Posts: 4
Joined: Wed Feb 13, 2013 8:59 am

Re: Format Characters

Postby casevh » Thu Feb 14, 2013 3:59 am

The various format codes are documented here:

http://docs.python.org/2/library/stdtyp ... operations

%d attempts to format a value as an integer number:

Code: Select all
>>> "%d" % 1
'1'
>>> "%d" % 1.1
'1'
>>> "%d" % "a"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: %d format: a number is required, not str


%s and %r use the "str()" and "repr()" functions to format an object. In many cases, those two representations are the same, but sometimes they differ.

Code: Select all
>>> from decimal import Decimal
>>> a=Decimal(123)
>>> str(a)
'123'
>>> repr(a)
"Decimal('123')"


%s is usually used to format an easy-to-read display. %r is usually used to format error messages since the additional information can be helpful. For example, %s produces the same output for both the integer 123 and Decimal(123) but %r will format them differently.

casevh
casevh
 
Posts: 68
Joined: Sat Feb 09, 2013 7:35 am

Re: Format Characters

Postby Mekire » Tue Feb 19, 2013 2:03 am

Thought I would just mention something about bracket notation and the format function here. Although far from deprecated, there has been a move to pull away from the old style % formatting in favor of the more recent format function. It of course ends up producing identical results, but I think it looks somewhat cleaner, and definitely feels more Pythonic.

So something like this:
Code: Select all
name = "Bob"
job = "Programmer"
pay = 30.35
print("Name:%s, Job:%s, Pay:$%.2f per hour" % (name,job,pay))
would now be printed like this:
Code: Select all
print("Name:{}, Job:{}, Pay:${:.2f} per hour".format(name,job,pay))
and both would have the output:
Code: Select all
Name:Bob, Job:Programmer, Pay:$30.35 per hour


This format function, as with the previous % style, has its own Format Specification Mini-Language

The choice between the two is still very much a matter of style, but having string formatting being a method of the string-type seems much more natural to me.

-Mek
User avatar
Mekire
 
Posts: 980
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: freddyhard and 2 guests