8

I have a RNA sequence of varying length. Max length of the sequence is 22. Some sequences are shorter. So if a sequence is shorter then add "0" to the end of the line till the length becomes 22.

How to do it in python? this is my input

AAGAUGUGGAAAAAUUGGAAUC

CAGUGGUUUUAUGGUAG

CUCUAGAGGGUUUCUG

UUCAUUCGGC

and my expected ouput should be for example in the last line it does not contain 22 characters so i need to make it 22 by adding zero to it

UUCAUUCGGC000000000000

but as per your commands i am getting out put as AAGAUGUGGAAAAAUU 00000 the addtional characters used for justification came down and not in the same line

2
  • I see you're reading the lines from a file, use rstrip() to strip the newline characters('\n') first: s.rstrip().ljust(22, '0') Commented Apr 22, 2014 at 11:04
  • Worked ..Thanks a lot buddy... WIll be troubling u a lot Commented Apr 22, 2014 at 11:21

3 Answers 3

17

Use str.ljust method:

>>> s = 'foobar'
>>> s.ljust(22, '0')
'foobar0000000000000000'
Sign up to request clarification or add additional context in comments.

6 Comments

@Aswini Chaudhary : but one more problem is when i tried to use them in the code and print the extra zero are printed in next line and not appended to the line for line in file: if not line.startswith (">"): if len(line) > 22: # maximum length of the miR sequence pass else: for line1 in line: print line.ljust(22,'0'); and the output came like AAGAUGUGGAAAAAUU 00000 AAGAUGUGGAAAAAUU 00000
@ramko You shouldn't iterate over the string, simply do: line1.ljust(22,'0')
@ramko Please post your sample data and expected output in the question body, comments are not readable.
@ Aswini Chadhary : Posted as requested
|
9

An alternative to @Aशwini चhaudhary's answer is:

s = '{0:0<22}'.format(s)

Example

s = 'hello'
s = '{0:0<22}'.format(s)

>>> print s
hello00000000000000000

Alternative

Or as शwini चhaudhary suggested, simply:

s = format(s, '0<22')

1 Comment

@Aशwiniचhaudhary, Thank you, I have included it in my answer.
0

you can try it using while loop. Lets say you have all the sequences in a list.

x=['AAGAUGUGGAAAAAUUGGAAUC','CAGUGGUUUUAUGGUAG','CUCUAGAGGGUUUCUG','UUCAUUCGGC']

now you can run a while loop on the length of the mrna sequence. This loop will check the length of mrna and will continue adding '0' at the end until the length of sequence is 22

new=''
for mrna in x:
    new=mrna
    while len(new)<22:
        new=new+'0'
    print(new)

while len(mrna)<22:
   mrna+'0'

output will be:

AAGAUGUGGAAAAAUUGGAAUC
CAGUGGUUUUAUGGUAG00000
CUCUAGAGGGUUUCUG000000
UUCAUUCGGC000000000000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.