简体   繁体   English

Python - 使字符串长度相等

[英]Python - make string equal length

I have a RNA sequence of varying length. 我有一个不同长度的RNA序列。 Max length of the sequence is 22. Some sequences are shorter. 序列的最大长度为22.一些序列较短。 So if a sequence is shorter then add "0" to the end of the line till the length becomes 22. 因此,如果序列较短,则在行的末尾添加“0”,直到长度变为22。

How to do it in python? 如何在python中做到这一点? this is my input 这是我的意见

AAGAUGUGGAAAAAUUGGAAUC AAGAUGUGGAAAAAUUGGAAUC

CAGUGGUUUUAUGGUAG CAGUGGUUUUAUGGUAG

CUCUAGAGGGUUUCUG CUCUAGAGGGUUUCUG

UUCAUUCGGC 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 我的预期输出应该是例如在最后一行它不包含22个字符所以我需要通过向它添加零来使它22

UUCAUUCGGC000000000000 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 但是按照你的命令,我把它作为AAGAUGUGGAAAAAUU 00000用于理由的附加字符下来而不是在同一行

Use str.ljust method: 使用str.ljust方法:

>>> s = 'foobar'
>>> s.ljust(22, '0')
'foobar0000000000000000'

An alternative to @Aशwini चhaudhary's answer is: @Aशwiniचhaudhary的替代方案是:

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: 或者正如शwiniचhaudhary建议的那样,简单地说:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果长度不等于x,则Python将字符串替换为空 - Python replace string with empty if length not equal to x Python 记录日志级别的等长字符串 - Python Logging Equal Length String of Log Level 在Python中将字符串长度除以6 - Make string length divisible by 6 in Python Javascript string.length 不等于 Python len() - Javascript string.length does not equal Python len() 在Python中将2个字符置换成固定长度的字符串,每个字符的数目相等 - Permutations of 2 characters in Python into fixed length string with equal numbers of each character 如何将字符串拆分为长度相等的对,python 中的空位置为 0? - How to split a string into pairs of equal length with 0 in the empty positions in python? sizeof(string)不等于字符串长度 - sizeof(string) not equal to string length Python3:使itemgetter按字符串长度排序 - Python3: Make itemgetter sort by string length Python - 密文长度必须等于密钥大小 - 将其传递给服务器后不同的字符串长度 - Python - Ciphertext length must be equal to key size - different string length after passing it to server 生成长度等于文件中时间长度的字符串,每秒1个标签,python - generate string with length equal to length of time in file, with 1 label per second , python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM