[Tutor] Working with strings

alan.gauld@bt.com alan.gauld@bt.com
Mon, 12 Mar 2001 16:52:07 -0000


------_=_NextPart_001_01C0AB14.C600A300
Content-type: text/plain; charset="iso-8859-1"

it at the moment). Can you correct my program below and give me some sample
program as to how to do it simply. I'm not having trouble with the logic of
the string manipulation(I think) - just getting the program to accept input
and print output.
 

Most replies have tried to give you a different function, I'll try to 
work with this one...  
 
First I'll indent it they way I think you meant it to be and use a non 
proportional font for clarity:
 
def backword(bruce):
    bruce=raw_input('input a word to reverse  ')
    index=len(bruce)-1
    while index<0:
       letter=bruce[index]
       print letter,
       index = index+1 
   
>>> backword
<function backword at 00BC015C>
 
So we've defined a function which takes an argument, now to see if it
works...
 
 >>> backword(flowerpot)
Traceback (innermost last):
  File "<pyshell#36>", line 1, in ?
    backword(flowerpot)
NameError: There is no variable named 'flowerpot' 
 
We passed a variable parameter(not a string because there were no quote
signs 
around it) and Python objected because the variable doesn't exist yet.. 
Fix it by declaring the variable before calling the function:
 
>>> flowerpot = "Flowerpot"
>>> backward(flowerpot)
 
Or by calling the function with a string:
 
>>> backward("flowerpot") # note the quotes...
 
 
 
>>> backword('flowerpot')
input a word to reverse  flowerpot 
 
OK, we entered the function. Now its asking us to replace the argument we 
passed('flowerpot') with a new value we type at the prompt.
 We now try to execute:
 
    index=len(bruce)-1
find index of last letter

    while index<0:
never do the loop stuff because the index of the last letter is greater than
0
exit the function.
 
>>> print bruce
harold # a value I entered earlier that it hasn't cleared. 
correct!
 
>>> def backword('bruce'):
 SyntaxError: invalid syntax 
 
Try to define a new function but specify an argumemnt name of "bruce" 
which is a string literal - Python doesn't allow string literals as
argument/variable 
names so its a syntax error.
 
>>> backword(flowerpot)
Traceback (innermost last):
  File "<pyshell#40>", line 1, in ?
    backword(flowerpot)
NameError: There is no variable named 'flowerpot'
 
Back to the start of the message :-)
 
HTH,
 
Alan G.

------_=_NextPart_001_01C0AB14.C600A300
Content-type: text/html; charset="iso-8859-1"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">


<META content="MSHTML 5.00.3013.2600" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<BLOCKQUOTE 
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
  <DIV><FONT face=Arial size=2>it at the moment). Can you correct my program 
  below and give me some sample program as to how to do it simply. I'm not 
  having trouble with the logic of the string manipulation(I think)&nbsp;- just 
  getting the program to accept input and print output.</FONT></DIV>
  <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV></BLOCKQUOTE>
<DIV><SPAN class=360213516-12032001><FONT color=#0000ff face=Arial size=2>Most 
replies have tried to give you a different function, I'll try to 
</FONT></SPAN></DIV>
<DIV><SPAN class=360213516-12032001><FONT color=#0000ff face=Arial size=2>work 
with this one...</FONT></SPAN>&nbsp;<FONT size=2><FONT face=Arial><SPAN 
class=360213516-12032001><FONT 
color=#0000ff>&nbsp;</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN 
class=360213516-12032001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN class=360213516-12032001><FONT 
color=#0000ff>First I'll indent it they way&nbsp;I think you meant it to be and 
use a non </FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN class=360213516-12032001><FONT 
color=#0000ff>proportional font for clarity:</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN 
class=360213516-12032001>&nbsp;</SPAN><BR></FONT><FONT 
face=Arial></FONT></FONT><FONT face="Courier New" size=2>def 
backword(bruce):<BR><SPAN class=360213516-12032001><FONT color=#0000ff><FONT 
color=#000000>&nbsp;&nbsp;</FONT>&nbsp;</FONT></SPAN>&nbsp;bruce=raw_input('input 
a word to reverse&nbsp; ')<BR><SPAN class=360213516-12032001><FONT 
color=#0000ff>&nbsp;<FONT 
color=#000000>&nbsp;</FONT>&nbsp;</FONT></SPAN>&nbsp;index=len(bruce)-1<BR>&nbsp;<SPAN 
class=360213516-12032001><FONT 
color=#0000ff>&nbsp;&nbsp;&nbsp;</FONT></SPAN>while 
index&lt;0:<BR>&nbsp;&nbsp;<SPAN class=360213516-12032001><FONT 
color=#0000ff>&nbsp;&nbsp;&nbsp; 
&nbsp;</FONT></SPAN>letter=bruce[index]<BR>&nbsp;&nbsp;<SPAN 
class=360213516-12032001><FONT color=#0000ff>&nbsp;&nbsp;&nbsp; 
&nbsp;</FONT></SPAN>print letter,<BR>&nbsp;&nbsp;<SPAN 
class=360213516-12032001><FONT color=#0000ff>&nbsp;&nbsp;&nbsp; 
&nbsp;</FONT></SPAN>index = index+1</FONT><FONT size=2><FONT face=Arial><SPAN 
class=360213516-12032001><FONT 
color=#0000ff>&nbsp;</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN 
class=360213516-12032001>&nbsp;</SPAN>&nbsp;&nbsp;<BR></FONT><FONT 
face="Courier New">&gt;&gt;&gt; backword<BR>&lt;function backword at 
00BC015C&gt;<BR><SPAN class=360213516-12032001><FONT 
color=#0000ff>&nbsp;</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=360213516-12032001>So we've defined a function which takes an argument, 
now to see if it works...</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=360213516-12032001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face="Courier New"><FONT size=2><SPAN 
class=360213516-12032001>&nbsp;</SPAN>&gt;&gt;&gt; 
backword(flowerpot)<BR>Traceback (innermost last):<BR>&nbsp; File 
"&lt;pyshell#36&gt;", line 1, in ?<BR>&nbsp;&nbsp;&nbsp; 
backword(flowerpot)<BR>NameError: There is no variable named 'flowerpot'<SPAN 
class=360213516-12032001><FONT 
color=#0000ff>&nbsp;</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN 
class=360213516-12032001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=360213516-12032001>We passed a variable parameter(not a string because 
there were no quote signs </SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=360213516-12032001>around it) and Python objected because the variable 
doesn't exist yet.. </SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=360213516-12032001>Fix it by declaring the variable before calling the 
function:</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=360213516-12032001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT size=2><FONT color=#0000ff face="Courier New"><SPAN 
class=360213516-12032001>&gt;&gt;&gt; flowerpot = 
"Flowerpot"</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face="Courier New"><SPAN 
class=360213516-12032001>&gt;&gt;&gt; 
backward(flowerpot)</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff><SPAN 
class=360213516-12032001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=360213516-12032001>Or by calling the function with a 
string:</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=360213516-12032001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT size=2><FONT color=#0000ff face="Courier New"><SPAN 
class=360213516-12032001>&gt;&gt;&gt; backward("flowerpot") # note the 
quotes...</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff><SPAN 
class=360213516-12032001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN 
class=360213516-12032001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN 
class=360213516-12032001>&nbsp;</SPAN><BR></FONT><FONT 
face="Courier New">&gt;&gt;&gt; backword('flowerpot')<BR>input a word to 
reverse&nbsp; flowerpot<SPAN class=360213516-12032001><FONT 
color=#0000ff>&nbsp;</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN 
class=360213516-12032001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN class=360213516-12032001><FONT 
color=#0000ff>OK, we entered the function. Now its asking us to replace the 
argument we </FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN class=360213516-12032001><FONT 
color=#0000ff>passed('flowerpot') with a new value we type at the 
prompt.</FONT></SPAN></FONT></FONT></DIV>
<DIV><SPAN class=360213516-12032001></SPAN><FONT size=2><FONT face=Arial><SPAN 
class=360213516-12032001><FONT color=#0000ff>&nbsp;We now try to 
execute:</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN 
class=360213516-12032001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face="Courier New"><FONT size=2><SPAN class=360213516-12032001><FONT 
color=#0000ff><SPAN class=360213516-12032001><FONT color=#0000ff>&nbsp;<FONT 
color=#000000>&nbsp;</FONT>&nbsp;</FONT></SPAN>&nbsp;index=len(bruce)-1</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><SPAN class=360213516-12032001><FONT color=#0000ff 
face=Arial>find index of last letter</FONT></SPAN></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN class=360213516-12032001><FONT 
color=#0000ff><BR><FONT face="Courier New">&nbsp;<SPAN 
class=360213516-12032001><FONT 
color=#0000ff>&nbsp;&nbsp;&nbsp;</FONT></SPAN>while index&lt;0:<BR></FONT><FONT 
face=Arial>never do the loop stuff  because the index of the last letter is 
greater than 0</FONT></FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff face=Arial><SPAN 
class=360213516-12032001>exit the function.</SPAN></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><SPAN 
class=360213516-12032001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><FONT face="Courier New">&gt;&gt;&gt; print 
bruce<BR></FONT>harold # a value I entered earlier that it hasn't cleared.<SPAN 
class=360213516-12032001><FONT color=#0000ff>&nbsp;</FONT></SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN 
class=360213516-12032001>correct!</SPAN></FONT></DIV>
<DIV><FONT face=Arial><FONT size=2><SPAN 
class=360213516-12032001>&nbsp;</SPAN><BR><FONT face="Courier New"></FONT><FONT 
size=2>&gt;&gt;&gt; def backword('bruce'):<BR></FONT></FONT><FONT 
size=2>&nbsp;SyntaxError: invalid syntax<SPAN class=360213516-12032001><FONT 
color=#0000ff>&nbsp;</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT face=Arial><FONT size=2><SPAN 
class=360213516-12032001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial><FONT color=#0000ff size=2><SPAN 
class=360213516-12032001>Try to define a new function but specify an argumemnt 
name of "bruce" </SPAN></FONT></FONT></DIV>
<DIV><FONT face=Arial><FONT color=#0000ff size=2><SPAN 
class=360213516-12032001>which is a string literal - Python doesn't allow string 
literals as argument/variable </SPAN></FONT></FONT></DIV>
<DIV><FONT face=Arial><FONT color=#0000ff size=2><SPAN 
class=360213516-12032001>names so its a syntax error.</SPAN></FONT></FONT></DIV>
<DIV><FONT face=Arial><FONT color=#0000ff size=2><SPAN 
class=360213516-12032001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&gt;&gt;&gt; backword(flowerpot)<BR>Traceback 
(innermost last):<BR>&nbsp; File "&lt;pyshell#40&gt;", line 1, in 
?<BR>&nbsp;&nbsp;&nbsp; backword(flowerpot)<BR>NameError: There is no variable 
named 'flowerpot'<BR><SPAN class=360213516-12032001><FONT 
color=#0000ff>&nbsp;</FONT></SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=360213516-12032001>Back 
to the start of the message :-)</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN 
class=360213516-12032001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN 
class=360213516-12032001>HTH,</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN 
class=360213516-12032001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=360213516-12032001>Alan 
G.</SPAN></FONT></DIV></BODY></HTML>

------_=_NextPart_001_01C0AB14.C600A300--