[Tutor] special translations

Kojo Idrissa kojo@hal-pc.org
Mon, 21 May 2001 03:32:00 -0500


--=====================_164306==_.ALT
Content-Type: text/plain; charset="us-ascii"; format=flowed

Cameron,

Are you trying to actually replace the space in the string with a '~', or 
do you just want to print a '~' in place of the space when the string is 
printed, but leave the string with the spaces in place?

I seem to have written a minor tongue-twister...

If you want to replace the space (alter the actual string), Andrew's 
suggestion makes the most sense.

If you want to keep the string full of spaces, but print '~' in their 
place, (which is what it sounds like you're trying to do) there would seem 
to be a couple of options (at least at 3am...):
1.  Use replace.string to create copies of the strings for printing
         example_string='abc def ghi jkl'
         print_string=example_string.replace(' ','~')
         print print_string
2. Iterate through the strings element by element and determine if the 
character in question is a space or not.  If not, print it, if so, print 
'~'.  NB: what follows is intended to be Psuedo-Code.

         x=0
         while example_string:
             if example_string[x] != '~':
               print example_string[x]
               x=x+1
         else:
           print '~'
           x=x+1

Without knowing more about what you're trying to do, I'm not sure which 
would be more efficient.  Actually, even if I did know what you were doing, 
I might not know.  I will defer to the expertise of those who know more 
than I.  (That means just about everyone on the list.)
:-)

Am I on the right track as far as what you're trying to do?

Hope this helps,


At 06:50 PM 5/20/2001 -0500, you wrote:
>Hi to all Python people,
>
>I wanted to know if anyone knows how to tell python to take a space you 
>left and turn it into a character.
>
>ex:
>if ltter == 'space':
>         print '~'
>When Ieave a space in the above space part in the code it gives me an 
>error.  How can you tell python that the space is okay and for it to print 
>the symbol.
>
>-Cameron

****************************
Kojo Idrissa

kojo@hal-pc.org
http://www.hal-pc.org/~kojo/
****************************
--=====================_164306==_.ALT
Content-Type: text/html; charset="us-ascii"

<html>
Cameron,<br>
<br>
Are you trying to actually replace the space in the string with a '~',
<b>or </b>do you just want to print a '~' in place of the space when the
string is printed, but leave the string with the spaces in place?<br>
<br>
I seem to have written a minor tongue-twister...<br>
<br>
If you want to replace the space (alter the actual string), Andrew's
suggestion makes the most sense.&nbsp; <br>
<br>
If you want to keep the string full of spaces, but print '~' in their
place, (which is what it sounds like you're trying to do) there would
seem to be a couple of options (at least at 3am...):<br>
1.&nbsp; Use replace.string to create copies of the strings for
printing<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab><font face="tahoma" size=2>example_string='abc
def ghi jkl'<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>print_string=example_string.replace('
','~')<br>
</font><x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>print
<font face="tahoma" size=2>print_string<br>
</font>2. Iterate through the strings element by element and determine if
the character in question is a space or not.&nbsp; If not, print it, if
so, print '~'.&nbsp; NB: what follows is intended to be 
Psuedo-Code.<br>
<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>x=0<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>while
example_string:<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>&nbsp;&nbsp;&nbsp;
if example_string[x] != '~':<br>
&nbsp;
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
print example_string[x]<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
x=x+1<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>else:<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>&nbsp;
print '~'<br>
<x-tab>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</x-tab>&nbsp;
x=x+1<br>
<br>
Without knowing more about what you're trying to do, I'm not sure which
would be more efficient.&nbsp; Actually, even if I did know what you were
doing, I might not know.&nbsp; I will defer to the expertise of those who
know more than I.&nbsp; (That means just about everyone on the
list.)<br>
:-)<br>
<br>
Am I on the right track as far as what you're trying to do?<br>
<br>
Hope this helps,<br>
<br>
<br>
At 06:50 PM 5/20/2001 -0500, you wrote:<br>
<blockquote type=cite class=cite cite><font face="arial" size=2>Hi to all
Python people,</font><br>
&nbsp;<br>
<font face="arial" size=2>I wanted to know if anyone knows how to tell
python to take a space you left and turn it into a 
character.</font><br>
&nbsp;<br>
<font face="arial" size=2>ex:</font><br>
<font face="arial" size=2>if ltter == 'space':<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print '~'</font><br>
<font face="arial" size=2>When Ieave a space in the above space part in
the code it gives me an error.&nbsp; How can you tell python that the
space is okay and for it to print the symbol.</font><br>
&nbsp;<br>
<font face="arial" size=2>-Cameron</font></blockquote>
<x-sigsep><p></x-sigsep>
**************************** <br>
Kojo Idrissa <br>
&nbsp; <br>
kojo@hal-pc.org<br>
<a href="http://www.hal-pc.org/~kojo/" eudora="autourl">http</a>://www.hal-pc.org<a href="http://www.hal-pc.org/~kojo/" eudora="autourl">/~kojo/</a><br>
****************************</html>
--=====================_164306==_.ALT--