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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值