|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
It should be very simple, but I seem to be missing it. In perl it would look like this:
$name = (whatever name you input) $age = (again, whatever you input previously) then you could display it like: print "Hello $name, you are $age years old." I know python could do print "Hello",name,"you are",age,"years old." But that doesnt cut it, since it always adds spaces. print "Hello",name,"!" whould print: Hello Jack ! Dont ask me why python forces spaces in there.. is there anyway to just call variables within strings like in perl? |
|
#2
|
|||
|
|||
|
It is very simple: Just use string format characters.
In this example: print 'Hello %s, you are %s years old.' % (name, age) You could also use string concatenation like this: print 'Hello '+name+', you are '+age+' years old.' But this is slower, and IMO less readable.
__________________
Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems. - Jamie Zawinski, in comp.lang.emacs |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > In python, how do you put variables in strings? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|