The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
In python, how do you put variables in strings?
Discuss In python, how do you put variables in strings? in the Python Programming forum on Dev Shed. In python, how do you put variables in strings? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 3rd, 2002, 03:39 PM
|
|
Registered User
|
|
Join Date: Feb 2002
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
In python, how do you put variables in strings?
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?
|

February 4th, 2002, 06:31 AM
|
|
Python Prophet
|
|
Join Date: Jun 2001
Location: Amersfoort, The Netherlands
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 12
|
|
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
|

December 18th, 2012, 08:17 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 6
Time spent in forums: 26 m 34 sec
Reputation Power: 0
|
|
|
why can't you use %u instead of %s?
|

December 18th, 2012, 08:50 PM
|
 |
Contributing User
|
|
|
|
|
user input
User input from terminal changed between python2 and python3. This method works in both versions:
Code:
import sys
sys.stdout.write('OK? (y/n) ')
user_input = sys.stdin.readline()
if 'y' == user_input[0].lower():
ok_action()
else:
no_go()
__________________
[code] Code tags[/code] are essential for python code!
|

December 19th, 2012, 12:54 PM
|
 |
Contributing User
|
|
|
|
Code:
name = 'Frank'
age = 12
# vars() is the local dictionary containing variables name and age as keys
print("Hello %(name)s, you are %(age)s years old." % vars())
''' result -->
Hello Frank, you are 12 years old.
'''
A little bit more old-fashioned:
Code:
import string
name = "Frank"
age = 12
# variables to be substituted begin with a $
t = string.Template("Hello $name, you are $age old!")
# local disctionary vars() contains variables name and age
s = t.substitute(vars())
print(s)
''' result -->
Hello Frank, you are 12 old!
'''
If you have Python273 or Python3, you can use:
Code:
name = 'Frank'
age = 12
# vars() is the local dictionary containing variables name and age as keys
# needs Python273 or Python3 and higher
print("Hello {name}, you are {age} years old.".format(**vars()))
''' result -->
Hello Frank, you are 12 years old.
'''
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
Last edited by Dietrich : December 19th, 2012 at 01:51 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|