[Tutor] modulus

ADRIAN KELLY kellyadrian at hotmail.com
Wed Nov 16 17:09:04 CET 2011


really appreciate that answer thanks very much..........

 
  
Adrian Kelly 
1 Bramble Close
Baylough
Athlone
County Westmeath

0879495663

 



From: waynejwerner at gmail.com
Date: Wed, 16 Nov 2011 09:59:50 -0600
Subject: Re: [Tutor] modulus
To: kellyadrian at hotmail.com
CC: tutor at python.org


On Wed, Nov 16, 2011 at 9:46 AM, ADRIAN KELLY <kellyadrian at hotmail.com> wrote:



Please can anyone tell me how i can print this without all the brackets and commas, i know i need the modulus symbol but i dont know how it works.  
any advice would be appreciated
 
regards
adrian
 
def arguments():
    name=raw_input ("Please enter your firstname: ")
    surname=raw_input ("Enter your surname: ")
    address1=raw_input ("Enter Address line 1: ")
    address2=raw_input ("Enter Address line 2: ")
    address3=raw_input ("Enter Address line 3: ")
    return name,surname,address1,address2,address3
address=arguments()
print "%s %s" % address



In this case it's not actually modulus, it's just the syntax for string formatting. I'm not sure *what* the reasoning behind the % was, but that's the way it is.


There are two ways to do string formatting, the new (.format) and old (%).


In new style formatting you use the .format method of the string:


"{0} {1} {2}".format("One", 2, "Five")


You don't usually have to worry about the type, though you can specify it along with some other useful modifiers for precision, spacing, and alignment. 


In old style formatting, you use a string with format specifiers (%s, %d, etc.) followed by a tuple of arguments. Here, the lengths have to match exactly - if you have one specifier then you must have a 1-element tuple. In your case, you're returning a 5 element tuple, so you want 5 format specifiers:


print "%s %s %s %s %s" % address


However, if you just want to print the data out like that you can do it a little easier like this:


print ' '.join(address)


Or if you are in 3.x or use `from __future__ import print_function` then you can do this:


print(*address)


HTH,
Wayne 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111116/507cb269/attachment-0001.html>


More information about the Tutor mailing list