Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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