Hallo ...

I am very new to Python programming & I have this Question ...

I am asked to write a program to calculate the No. of words & the length of each (letters) in this sentence :
(An Oligonucleotide is a short segment of RNA or DNA, typically with twenty or fewer bases. Although they can be formed by cleavage of longer segments,
they are now more commonly synthesized by polymerizing individual nucleotide
precursors. Automated synthesizers allow the synthesis of oligonucleotides up to 160
to 200 bases) & here how I did it :

s="""An Oligonucleotide is a short segment of RNA or DNA, typically with twenty or fewer bases. Although they can be formed by cleavage of longer segments,
they are now more commonly synthesized by polymerizing individual nucleotide
precursors. Automated synthesizers allow the synthesis of oligonucleotides up to 160
to 200 bases"""
s=s.replace('\n','')
print s
a=s.split()
print "Number of words =",len(a)
for i in a :
b=len(i)
print b

My program working fine ,But I want to modify it ,& to print infront of each number ,the words that indicate its length for example :( An =2 ,oligonucleotide=15 & so ...) or i also tried to print ( the letters No.in the word No.1 =2 , the letters No. in the word No.2 =15 & so on for the rest of the 47 words)

I started to make a loop for the words no . like this

s=s.replace('\n','')
print s
a=s.split()
print "Number of words =",len(a)
for i in a :
q=0 # to creat a loop for the number of the words
while (q<48):
q+=1
b=len(i)
print "the No.of letters in word No",q,b # the statment of "the No of letters..." should be the same but with changing No .& that is what Q should point to like : 1,2,3,4,.....till 47

But it didnt work !!any possible help or hint ?

Please wrap your program in [code] Your program goes here [/code] tags. Otherwise we can't see how the lines in your program are indented and have to guess. Also when you say it didn't work, do you mean that you got no output, or that the output was not what you wanted? If you got some output please show us what it looked like.

How about this if your code is right (need only simple length, not need to count each word only once, sort or something)
((code) button is useful for posting readable programs):

s="""An Oligonucleotide is a short segment of RNA or DNA, typically with twenty or fewer bases. Although they can be formed by cleavage of longer segments,
they are now more commonly synthesized by polymerizing individual nucleotide
precursors. Automated synthesizers allow the synthesis of oligonucleotides up to 160
to 200 bases"""
s=s.replace('\n',' ').replace(',','').replace('.','')
print s
a=s.split()
print "Number of words =",len(a)
for w,i in enumerate(a) :
    print w+1,':',i,len(i)

D5e5 Sorry for not using (CODE) ,as I did nt know that ....

Tonyjv ,Yes that is just what I wanted .... thanks lot But there is only a minor problem ..in executing the following program ...there is 2 words counted as one word & separated by a comma (,)
while they are 2 different words ...the words are (segments they )so how can this be avoided ..( word no 26 in executing the program )

s="""An Oligonucleotide is a short segment of RNA or DNA, typically with twenty or fewer bases. Although they can be formed by cleavage of longer segments,
they are now more commonly synthesized by polymerizing individual nucleotide
precursors. Automated synthesizers allow the synthesis of oligonucleotides up to 160
to 200 bases"""
s=s.replace('\n','') # what is the benefit of ( replace ('.','').replace('.','')         
print s                 
a=s.split()
print "Number of words =",len(a)
for w,i in enumerate (a):
    print w+1,"No.of letters in the word :",i,":",len(i)

What you mean word number 26 is segments and 27 is they:

An Oligonucleotide is a short segment of RNA or DNA typically with twenty or fewer bases Although they can be formed by cleavage of longer segments they are now more commonly synthesized by polymerizing individual nucleotide precursors Automated synthesizers allow the synthesis of oligonucleotides up to 160 to 200 bases
Number of words = 50
1 : An 2
2 : Oligonucleotide 15
3 : is 2
4 : a 1
5 : short 5
6 : segment 7
7 : of 2
8 : RNA 3
9 : or 2
10 : DNA 3
11 : typically 9
12 : with 4
13 : twenty 6
14 : or 2
15 : fewer 5
16 : bases 5
17 : Although 8
18 : they 4
19 : can 3
20 : be 2
21 : formed 6
22 : by 2
23 : cleavage 8
24 : of 2
25 : longer 6
26 : segments 8
27 : they 4
28 : are 3
29 : now 3
30 : more 4
31 : commonly 8
32 : synthesized 11
33 : by 2
34 : polymerizing 12
35 : individual 10
36 : nucleotide 10
37 : precursors 10
38 : Automated 9
39 : synthesizers 12
40 : allow 5
41 : the 3
42 : synthesis 9
43 : of 2
44 : oligonucleotides 16
45 : up 2
46 : to 2
47 : 160 3
48 : to 2
49 : 200 3
50 : bases 5
>>>

Looks like you changed the replacement for '\n' to '' instead of ' '

Waoo ..Yes that is right ..thanks a lot ....it sounds for you these python problems trivial while for me ...not at all i spent lot of time trying to reach to what I ve reached in the 1st time & didnt make it perfect as you did !!

thanks

A more clearer an Pythonic way to print the last line and some better variable name.
This is done to make code more readably that always count as a plus.

s.replace('\n', '') you only need if you read in from a file.

s = """An Oligonucleotide is a short segment of RNA or DNA, typically with twenty or fewer bases. Although they can be formed by cleavage of longer segments,
they are now more commonly synthesized by polymerizing individual nucleotide
precursors. Automated synthesizers allow the synthesis of oligonucleotides up to 160
to 200 bases"""
                
word_split = s.split()
print word_split  #Test print.
# Look at the list and see if you happy with it
# 'DNA,' 'bases.' this is count as 2 word so ",." should not needed to be replaced.

print "Number of words =", len(word_split)
for letter, word in enumerate (word_split):   
    print '%d Number of letters in the word is: %s : %d'  % (letter + 1, word, len(word))

Snipsat: Your solution ignores punctuation. My solution is planned to be expanded later by using translate(None, for more complete solution. For example as list comprehension one liner.
You have chosen also very missleading variable name for word number: letter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.