python-字符串应用

1 字符串中某元素计数

2 字符串替换

3 字符串反向输出【注意python最右端是开区间】

例1:计算碱基A,T,G,C的数量 s='ATGCATGCCGTAATGCGCTA'

1 s='ATGCATGCCGTAATGCGCTA'
2 print(s.count('A'))
3 print(s.count('G'))
4 print(s.count('C'))
5 print(s.count('T'))
[root@localhost ~]# python 1.py
5
5
5
5

例2:将DNA连中的T碱基替换成RNA链中的U碱基

1 DNA_str='GATGGAACTTGACTACGTAAATT'
2 RNA_str=DNA_str.replace('T','U')
3 print(RNA_str)
1 [root@localhost ~]# python 1.py
2 GAUGGAACUUGACUACGUAAAUU

replace():字符串替换

语法:str.replace('old','new',max)

old:表示要被替换的字符串

new:替换成old的字符串

max:替换次数不超过max

1 a='this is a string ,this is not a string'
2 b=a.replace('is','was',3)
3 print(b)
[root@localhost ~]# python 1.py
thwas was a string ,thwas is not a string

方法二:利用模块

字符串反转

例1

1 a='AAAACCCGGT'
2 b=a[::-1]
3 print(b)
4 c=a[10::-1]
5 print(c)
1 [root@localhost ~]# python 1.py
2 TGGCCCAAAA
3 TGGCCCAAAA
原文地址:https://www.cnblogs.com/wujiadong2014/p/4917178.html