Jump to content

New to Python

- - - - -

  • Please log in to reply
1 reply to this topic

#1
mmccreesh1

mmccreesh1

    Newbie

  • Members
  • Pip
  • 4 posts
Hi, I am learning Python as my first programming language and built a "VERY" simple program.

age = input("What age are you: ")

agestring = str(age)

print "You are " + agestring

Although, I saw that their is an easier way to convert a numerical value to a string without having to use the str() function.
I though all you had to do is this

age = input("What age are you: ")

print "You are " + 'age'

But this just recongnises the word age as a string, anyone know how you do this?

Thanks.

#2
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
  • Location:Missouri
Two ways off the top of my head would be (using 2.6)

print "you are", age

or
print "you are %d" % age

The reason 'age' is recognized as a string is because python uses "" and '' to encapsulate strings that way you can do stuff like this
print 'she said "how old are you"'

"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users