[Tutor] String as a Variable?

Lindsay Davies Lindsay.Davies@moonshine.co.uk
Fri, 2 Feb 2001 18:39:11 +0000


--============_-1230983297==_ma============
Content-Type: text/plain; charset="us-ascii" ; format="flowed"

Why not use a dictionary?

snv = {'sn_1' : 123, 'sn_2' : 234, 'sn_3' : 345}
serial_number = '3'

try:
	print snv['sn_' + serial_number]
except KeyError, missing_key:
	print "The serial number '%s' is missing." % serial_number


Best wishes,

Lindsay


On 2/2/01, Seelinger, Bruce wrote about '[Tutor] String as a Variable?':
>Another newbie question...
>
>Is there a way to have a string represent a variable.
>
>For eaxample, I have a the following:
>
><snip>
>
>serial_number = '3'
>print 'ip_address_serial_' + serial_number
>ip_address_serial_3
>
>Now I want to use the string ip_address_serial_3 to pull
>the value assigned to the variable of the same name.
>
>How can I represent the string as a variable?
>
>Thanks in advance.

--============_-1230983297==_ma============
Content-Type: text/html; charset="us-ascii"

<!doctype html public "-//W3C//DTD W3 HTML//EN">
<html><head><style type="text/css"><!--
blockquote, dl, ul, ol, li { margin-top: 0 ; margin-bottom: 0 }
 --></style><title>Re: [Tutor] String as a
Variable?</title></head><body>
<div>Why not use a dictionary?</div>
<div><br></div>
<div><font color="#000000">snv = {</font><font
color="#007F00">'sn_1'</font><font color="#000000"> :
123,</font><font color="#007F00"> 'sn_2'</font><font color="#000000">
: 234,</font><font color="#007F00"> 'sn_3'</font><font
color="#000000"> : 345}<br>
serial_number =</font><font color="#007F00"> '3'</font></div>
<div><font color="#000000"><br>
<b>try</b>:<br>
<b><x-tab>&nbsp;&nbsp;&nbsp; </x-tab>print</b> snv[</font><font
color="#007F00">'sn_'</font><font color="#000000"> +
serial_number]<br>
<b>except</b> KeyError, missing_key:</font></div>
<div><font
color="#000000"><b><x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</x-tab>print</b></font><font color="#007F00"> &quot;The serial number
'%s' is missing.&quot;</font><font color="#000000"> %
serial_number</font></div>
<div><br></div>
<div><br></div>
<div>Best wishes,</div>
<div><br></div>
<div>Lindsay</div>
<div><br></div>
<div><br></div>
<div>On 2/2/01, Seelinger, Bruce wrote about '[Tutor] String as a
Variable?':</div>
<blockquote type="cite" cite>Another newbie question...<br>
<br>
Is there a way to have a string represent a variable.<br>
<br>
For eaxample, I have a the following:<br>
<br>
&lt;snip&gt;<br>
</blockquote>
<blockquote type="cite" cite>serial_number = '3'<br>
print 'ip_address_serial_' + serial_number<br>
ip_address_serial_3<br>
<br>
Now I want to use the string ip_address_serial_3 to pull<br>
the value assigned to the variable of the same name.</blockquote>
<blockquote type="cite" cite><br></blockquote>
<blockquote type="cite" cite>How can I represent the string as a
variable?</blockquote>
<blockquote type="cite" cite><br></blockquote>
<blockquote type="cite" cite>Thanks in advance.</blockquote>
<div><br></div>
</body>
</html>
--============_-1230983297==_ma============--