[Tutor] need help with string

Andrei Kulakov ak@silmarill.org
Sat, 18 May 2002 19:16:42 -0400


On Fri, May 17, 2002 at 06:26:16PM -0500, Dinakar Desai wrote:
> I have a long string say 80 characters and want to split into exactly 10
> chars except last one. Last one can be 10 or less characters. I could do
> it in  perl but want to convert that function from perl to python. I
> have looked in string and re modules. I could not find anything
> comparable.
> 
> I am not proficient in perl or python. Please bear with me.
> 
> Here is my perl code snippet:
> #!/usr/bin/env perl
> #
> use strict;
> my $strg = "This is very long string
> dfjksajdfkjskajfkjsdkajfksdjkfjkdjsakfjkdsjakfjsdkj";
> my @arr_str;
> 
> my $ind = 0;
> for (my $i = 0; $i < length($strg); $i +=10)
> {
>         $arr_str[$ind++] = substr($strg,$i, 10);
> }
> foreach my $f (@arr_str)
> {
>         print "$f\n";
>         }
> 
> print "@arr_str";
> 
> 
> Thank you.
> 
> Dinakar
>

>>> s = 't'*75
>>> s
'ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt'
>>> lst = []
>>> while 1:
...  chunk, s = s[:10], s[10:]
...  lst.append(chunk)
...  if not s:
...   break
... 
>>> lst
['tttttttttt', 'tttttttttt', 'tttttttttt', 'tttttttttt', 'tttttttttt', 'tttttttttt', 'tttttttttt', 'ttttt']

I don't know perl (I have vague memories from a few years back but your code
didn't ring a bell), so this may be a bit off. Do you want to end up with the
list of substrings?

 - Andrei
> 
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org