₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,326,375 members, 8,426,265 topics. Date: Sunday, 14 June 2026 at 12:21 AM

Toggle theme

New To Python, Gurus Please Help Solve These Challenges. Thanks. - Programming (2) - Nairaland

Nairaland ForumScience/TechnologyProgrammingNew To Python, Gurus Please Help Solve These Challenges. Thanks. (4350 Views)

1 2 Reply (Go Down)

Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(op): 10:14pm On Sep 08, 2018
Homguy:
hello friends,
please, how can I solve this puzzle?
I want to break a string with several letters and no spaces into a list. what method can I call on it?
for example:
card = 'AGDTDGATDGATDGATGDTGADGATDGATDGTAGATDGATDGATDGADTGADGTADGTAGDTADGTA'
how can I break this into a list with strings of 3 chars each? A list like ['AGD', 'TDG', 'ATD'...].


What I have done;
I have used the split() method but it does not work nor give the desired result, as it requires a delimiter and there's no sort of delimiter in this random code.
cc;
Snwokenk
Darivie04
dragnet
Modified:
So this is how i got the result i wanted using string indexing and reiteration as advised earlier.

hay = 'SAYPAYLAYKAYTAY'
n=3
print([hay[item:item +n] for item in range(0, len(hay), n)])
Result: ['SAY','PAY','LAY','KAY','TAY']
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by zyber(m): 1:55pm On Sep 09, 2018
Homguy:
hello friends,
please, how can I solve this puzzle?
I want to break a string with several letters and no spaces into a list. what method can I call on it?
for example:
card = 'AGDTDGATDGATDGATGDTGADGATDGATDGTAGATDGATDGATDGADTGADGTADGTAGDTADGTA'
how can I break this into a list with strings of 3 chars each? A list like ['AGD', 'TDG', 'ATD'...].


What I have done;
I have used the split() method but it does not work nor give the desired result, as it requires a delimiter and there's no sort of delimiter in this random code.
cc;
Snwokenk
Darivie04
dragnet
x = 0
while x < len(card):
print card[x:x+3]
x = x+3

Edit*
list = []
count = 0
while count < len(card):
n = card[count:count+3]
list.append(n)
count+=3
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Snwokenk: 8:24am On Oct 27, 2018
Homguy:
Modified:
So this is how i got the result i wanted using string indexing and reiteration as advised earlier.

hay = 'SAYPAYLAYKAYTAY'
n=3
print([hay[item:item +n] for item in range(0, len(hay), n)])
Result: ['SAY','PAY','LAY','KAY','TAY']
This is great! haven't been on Nairaland for a while. my apologies
1 2 Reply

Python Gurus In The House Help With This QuestionsPython Gurus Please Help Me With This Code.Nairaland Should Switch To Python/django. Opinion234

I'm About To Start My Programming Learning JourneyWeb Development Or Data AnalysticsUpwork Identify Verification