从字符串中查找特定字母并将其从该字符串中删除并打印出来 [英] To find a particular letter from a string and delete it from that string and print it

查看:94
本文介绍了从字符串中查找特定字母并将其从该字符串中删除并打印出来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这段代码,从字符串中找到字母'a'并删除它。有人可以纠正我的做法。



我尝试过:



 str = '  sonamkapoor' 

n = len(str)
c = 0;
字母 str:
while c< n:xmlns:n = #unknown>
如果 str [n] = ' a '
str [n] = str [n + 1 ]
c + 1

print (str)

解决方案

检查一下: Python String replace()方法 [ ^ ]

并将a替换为空字符串。

请参阅 5。数据结构& mdash; Python 3.3.6文档 [ ^ ]。

I have written this piece of code to find alphabet 'a' from the string and delete it. Can someone please correct my approach.

What I have tried:

str='sonamkapoor'

n=len(str)
c=0;
for letter in str:
    while c<n: xmlns:n="#unknown">
    if str[n]='a':
        str[n]=str[n+1]
        c+1

print(str)

解决方案

Check this out: Python String replace() Method[^]
and replace "a" with empty string.


See 5. Data Structures &mdash; Python 3.3.6 documentation[^].


这篇关于从字符串中查找特定字母并将其从该字符串中删除并打印出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆