Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

python- printing variables


  • Please log in to reply

#1
TaNkZ101

TaNkZ101

    Member

  • Member
  • PipPipPip
  • 327 posts
print "Welcome,", name,"!"
gives
Welcome, Hrvoje !
why is there whitespace, and how do i get rid of it (alternative?)?
the space between the space in ', name' doesn't seem to do anything, since there is no space in 'name,"!"', but one appears when executed.

(by the way, maybe you can quickly answer this too)
a = 2
b = 3
print a/b
how do i get it to print only two extra decimals (if it were 10/3 i'd want it to print 3.33). (in this case 0.66)

Edited by TaNkZ101, 30 July 2006 - 09:42 AM.

  • 0

Advertisements


#2
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP

why is there whitespace, and how do i get rid of it (alternative?)?

That's just the way the print function works. If you want to get rid of it you can concatenate all the strings using the + operator before printing them e.g.

print "Welcome," + name + "!"

a = 2
b = 3
print a/b

First of all, keep in mind that the / operator acts differently on integers than on floating point numbers. Integer division drops (truncates) any remainder. So 2/2 = 1 but 3/2 = 1 also. 2/3 = 0.

how do i get it to print only two extra decimals (if it were 10/3 i'd want it to print 3.33). (in this case 0.66)

Were you doing floating point division, you could use formatting statements to do this. See:
http://docs.python.o...eq-strings.html
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP